Is this ok?

B

Birt

to only initialize part of members?

class A
{
int m1;
int m2;
int m3
public:
A(v2, v3) : m2(v2), m3(v3) {}
};
 
K

Karl Heinz Buchegger

Birt said:
to only initialize part of members?

class A
{
int m1;
int m2;
int m3
public:
A(v2, v3) : m2(v2), m3(v3) {}
};

Technically there is nothing wrong as long
as you don't use m1 somewhere without
giving it a value before.

Personally I would give it a value in the
constructor too, just to make sure ....

A(v2, v3) : m1(0), m2(v2), m3(v3) {}
 
D

Daniel T.

Birt said:
to only initialize part of members?

class A
{
int m1;
int m2;
int m3
public:
A(v2, v3) : m2(v2), m3(v3) {}
};

Doing so creates an important piece of information that cannot be
expressed in code, the piece of information in question is "m1 is (not)
initialized". Anyone who uses m1 must be able to determine whether m1 is
initialized or not by looking at all the code that uses m1. Depending on
the size of your class and wether it exposes m1 in its interface, that
may be a daunting task for someone other than you, and may even be hard
for you a month from now after you have been working on something else.

Is there a particular reason you want to not initialize m1?
 
S

SaltPeter

Birt said:
to only initialize part of members?

class A
{
int m1;
int m2;
int m3
public:
A(v2, v3) : m2(v2), m3(v3) {}
};

You might consider specifying the types of your arguements in your cstor.
I'll second the other opinions about initializing m1, or for that matter,
any other variable not present in cstor arguements. Think about what happens
when another programmer reads or modifies your code.

#include <iostream>

class A
{
int m1;
int m2;
int m3;
public:
A(const int v2, const int v3) : m1(0), m2(v2), m3(v3) {}
virtual ~A() {}
void display() const
{
std::cout << "m1 is " << m1 << std::endl;
std::cout << "m1 is " << m2 << std::endl;
std::cout << "m1 is " << m3 << std::endl;
}
};

int main()
{
A a(5, 6);
a.display();

return 0;
}

Also, when you have a question, its helpful to show brief code that
compiles. Just showing us a class does not explain how you invoked the
object's cstor or your intentions. In this case, depending on your needs,
class A might have the 3 ints declared as:

int m1;
const int m2;
const int m3;

which implies that m2 and m3 variables are not mutable. Hence if a member
function attempts to modify these member variables, the compiler will
complain.
Therefore, only the cstor can modify the constant members. Code with
purpose.

Note that i've added a virtual destructor.
 
J

Julie

SaltPeter wrote:
Note that i've added a virtual destructor.

Which is only necessary if the class is intended to be derived from, otherwise
it should be considered inappropriate.
 
J

Julie

Birt said:
to only initialize part of members?

class A
{
int m1;
int m2;
int m3
public:
A(v2, v3) : m2(v2), m3(v3) {}
};

Perfectly fine to do this.

Note that you can initialize member data w/ non-parametric values as well:

//...
public:
A(v2, v3) : m1(-1), m2(v2), m3(v3) {}
};
 
V

Victor Bazarov

Julie said:
SaltPeter wrote:



Which is only necessary if the class is intended to be derived from, otherwise
it should be considered inappropriate.

Actually, it's only necessary if an object of a derived class is intended
to be deleted through a pointer to the base class (i.e. polymorphically).
If no such deletion is going to happen, a virtual destructor is unneeded.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top