This question already has answers here:
Do class functions/variables have to be declared before being used?
(5 answers)
Closed 5 years ago.
class cl {
public:
cl(int i) { val=i; }
int val;
int double_val() { return val+val; }
};
Variable val is declared after the constructor, which assigns it. But still this code works. Isn't 'val' out of scope for constructor?
The full definition of the class is available to its members. So val is actually declared before the constructor's implementation.
Related
This question already has answers here:
C++ class declaration after using it
(3 answers)
What are forward declarations in C++?
(8 answers)
Closed 3 months ago.
I'm sure that this has been asked, but I cannot find the question or answer, so here is the minimal code I tried to compile.
// goof4.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
class A;
class B
{
public:
A func() { return A{}; }
};
class A
{
};
int main()
{
B b;
auto a = b.func();
}
The declaration of B::func gives a "use of undefined type 'A' Error C2027 in MSVC 2022 using /std:c++20. I would have thought that the forward declaration of "class A" would have allowed the compiler to work on B::func until such time as class A was defined. Any help?
Because you have the function body using the (at that point) undefined type A in the class B itself and in a function body the type must already be defined.
just do A funct(); in the class B itself
and put the function body and after defining A,
A B::funct() { return A{}; }
https://ide.geeksforgeeks.org/2db37ea7-a62c-487b-8af5-10af8cebc3c6
This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
Closed 2 years ago.
#include<iostream>
using namespace std;
class Test {
int value;
public:
Test(int v = 0) {value = v;}
// We get compiler error if we add a line like "value = 100;"
// in this function.
int getValue() const {return value;}
};
int main() {
Test t(20);
cout<<t.getValue();
return 0;
}
Can anybody explain a typical practical scenario where Const function is necessary?
The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided.
A const member function can be called by any type of object. Non-const functions can be called by non-const objects only.
https://www.tutorialspoint.com/const-member-functions-in-cplusplus
This question already has answers here:
What is the purpose of the Most Vexing Parse?
(6 answers)
Closed 2 years ago.
I have a Request_manager.h file declaring an object Tickets_Queue as static in Request_manager, which is a nested class from itself.
class Request_manager {
public:
class Tickets_Queue{
private:
pthread_mutex_t m_mutex;
public:
Tickets_Queue(){};
~Tickets_Queue(){};
};
static Tickets_Queue ticket_queue;
private:
static int m_connected;
};
To initialize it, in Request_manager.cpp I write:
int Request_manager::m_connected(0);
Request_manager::Tickets_Queue Request_manager::ticket_queue();
The initialization of m_connected works but for ticket_queue it says:
gcc.archive core/bin/gcc-5.4.0/debug/link-static/threading-multi/libcore.a
gcc.compile.c++ data_interfaces/bin/gcc-5.4.0/debug/link-static/threading-multi/Request_manager.o
data_interfaces/Request_manager.cpp:17:62: error: no ‘dataserver::Request_manager::Tickets_Queue dataserver::Request_manager::ticket_queue()’ member function declared in class ‘dataserver::Request_manager’
Request_manager::Tickets_Queue Request_manager::ticket_queue();
You are calling a function.
Yeah. My mistake. As #john says it is prototyping a function.
You have to declare the type and the variable:
Request_manager::Tickets_Queue Request_manager::ticket_queue;
This question already has answers here:
What is this weird colon-member (" : ") syntax in the constructor?
(14 answers)
Closed 2 years ago.
what does the : e(data) do in the following code? Why are the curly brackets { } empty in the folowing code? Also can constant members of a class be initialized in this way?
Is this kind of definition specific to a constructor or it can be applied to all functions in C++?
class binaryfile
{
private:
const entry &e;
public:
binaryfile(const entry &data) : e(data){}
ostream& write(ostream &o)
{
o<<e.b_write();
}
}
binaryfile(const entry &data) : e(data){}
Defines a constructor which takes one argument and initializes the member variable e to that argument's value. The braces are empty because the constructor does nothing more.
It's called a member initializer list and it only works in constructor(s).
This question already has an answer here:
How to initialize a shared_ptr that is a member of a class?
(1 answer)
Closed 8 years ago.
I have a class as:
class LargeObject
{
public:
LargeObject();
void DoSomething();
private:
std::unique_ptr<Thing> pThing;
};
Then when I want to create the pointer in the constructor
LargeObject()
{
pThing(new Thing()); //This does not work.
}
I want to use the member variable throughout the code. How to do that?
I think initialization should be in constructor's initialization list, that's the place where constructors should be invoked from another constructor:
LargeObject()
:pThing(new Thing){}