This question already has answers here:
How do C++ class members get initialized if I don't do it explicitly?
(8 answers)
Value and size of an uninitialized std::string variable in c++
(5 answers)
Closed 2 years ago.
I have this string s and t in class.
class A
{
private:
static string s;
string t;
public:
void printString()
{
cout<<s<<t<<endl;
}
};
I tried printing these string and everytime they were empty if not initialized. So my question is whether a class string is always empty before initialization by a constructor. Also is it true for both static and non static strings?
In both cases, the default string constructor will be called. The default constructor for the std::string class will always initialize it to an empty string.
Related
This question already has answers here:
Static constant string (class member)
(11 answers)
How to initialize private static members in C++?
(18 answers)
Closed 2 years ago.
So it looks like I can't do this for some reason...
class MyClass
{
static std::string name = "Whatever";
};
I don't get it, for me it would make sense to define something in the declaration, even if is static, but instead, I have to do this... ¿?¿?¿?
class MyClass
{
static std::string name;
};
std::string MyClass::name = "Whatever";
And by the way... Isn't name private by default? Then why can I change its value from outside the class?
Why I can't define a static field in the declaration?
You can!
#include <string>
class MyClass
{
static inline std::string name = "Whatever";
// ^^^^^^
};
Isn't name private by default? Then why can I change its value from outside the class?
You can't!
You're defining/initialising it, not changing its value.
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 answers here:
Zero-Initialize array member in initialization list
(3 answers)
C++ Initializing Non-Static Member Array
(9 answers)
How to initialize all members of an array to the same value?
(26 answers)
Closed 3 years ago.
Suppose we have a class like the following one:
class myprogram {
public:
myprogram ();
private:
double aa,bb,cc;};
myprogram::myprogram():aa(0.0),bb(0.0),cc(0.0){}
As you can see we can initialize our private members' aa, bb, cc using the myprogram() constructor.
Now, suppose I have a large private array G_[2000]. how I could initialize all the values of this array equal to 0 using a constructor.
class myprogram {
public:
myprogram ();
private:
double aa,bb,cc;
double G_[2000];};
myprogram::myprogram():aa(0.0),bb(0.0),cc(0.0){}
Use std::memset function in constructor's body.
For example,
myprogram::myprogram()
: aa{0.0}, bb{0.0}, cc{0.0}
{
std::memset(G_, 0, 2000 * sizeof(double));
}
However, if you use braces {} in your initializer list, it will set default-initialize object (In case of array, it will fill it by zeroes).
You can write:
myprogram::myprogram()
{
for(int i=0;i<2000;i++)
G_[i]=0;
}
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){}
This question already has answers here:
Does a constructor / destructor have to have code, or is the function enough?
(3 answers)
Closed 9 years ago.
I have confusion regarding default and empty constructor. Does empty constructor also initializes class variable automatically ? Meaning if i use a empty constructor instead of default constructor , does that also initialize class member variable automatically ? For example, if use following code, does integer pointer is initialized to NULL ? Please confirm
// .h file
Class Test {
public:
Test();
~Test();
int *p;
}
// .cpp file
Test::Test()
{
// do something..
}
No, empty constructor is same as default constructor if you don't initialize any member variable inside it.