simple constructor problem

M

Michael

Hi All,

if I have a class that has a constant as an attribute like this:

class SomeClass {
private:
const int SOME_CONST;
int var;
public:
SomeClass() : SOME_CONST(4),var(0){} // default constructor...is
this right?
void someFunc();
};

if I do it like above I can do this:

SomeClass instance_name();

but I want to be able to call the constructor with a value for var like
this:

SomeClass instance_name(an_integer);

is it possible to do this without having to include a value for the const in
the function call?

Thanks for your help

Michael
 
A

Alf P. Steinbach

* Michael:
if I have a class that has a constant as an attribute like this:

class SomeClass {
private:
const int SOME_CONST;

Java'ism: in C++ reserve all uppercase names for macros.

Also note that instances of this class won't be assignable.

int var;
public:
SomeClass() : SOME_CONST(4),var(0){} // default constructor...is
this right?
Yes.


void someFunc();
};

if I do it like above I can do this:

SomeClass instance_name();

but I want to be able to call the constructor with a value for var like
this:

SomeClass instance_name(an_integer);

Just provide that constructor.

is it possible to do this without having to include a value for the const in
the function call?

ITYM 'constructor call'. Yes. You can either have two constructors
(one with and one without argument), or one constructor with an argument
with default value.
 
H

Heinz Ozwirk

Michael said:
Hi All,

if I have a class that has a constant as an attribute like this:

class SomeClass {
private:
const int SOME_CONST;
int var;
public:
SomeClass() : SOME_CONST(4),var(0){} // default constructor...is
this right?
void someFunc();
};

if I do it like above I can do this:

SomeClass instance_name();

Additionally to what Alf already wrote, you cannot do that. That declares a
function with no arguments, which is supposed to return an instance of
SomeClass. To define a variable of type SomeClass, simply write

SomeClass instance_name;

HTH
Heinz
 
M

Michael

Alf P. Steinbach said:
* Michael:

Java'ism: in C++ reserve all uppercase names for macros.

Also note that instances of this class won't be assignable.



Just provide that constructor.

for some reason I couldn't see how to do this yesterday....hence my
questioning if it was possible.....today it is obvious....

Thanks for the help :)
 
M

Michael

Ok, so here's another question:

is it better to declare the constructor body within the class declaration
like:

class SomeClass {
private:
const int SOME_CONST;
int var;
public:
SomeClass() : SOME_CONST(4),var(0){}
SomeClass(int i) : SOME_CONST(4),var(i){}
int getVar();
};

or just declare the prototype and then do the body later like:

class SomeClass {
private:
const int SOME_CONST;
int var;
public:
SomeClass();
SomeClass(int i);
int getVar();
};

SomeClass::SomeClass() : SOME_CONST(4),var(0){}
SomeClass::SomeClass(int i) : SOME_CONST(4),var(i){}

and why?

Thanks for your help

Regards

Michael
 
K

Kai-Uwe Bux

Michael said:
Ok, so here's another question:

is it better to declare the constructor body within the class declaration
like:

class SomeClass {
private:
const int SOME_CONST;
int var;
public:
SomeClass() : SOME_CONST(4),var(0){}
SomeClass(int i) : SOME_CONST(4),var(i){}
int getVar();
};

or just declare the prototype and then do the body later like:

class SomeClass {
private:
const int SOME_CONST;
int var;
public:
SomeClass();
SomeClass(int i);
int getVar();
};

SomeClass::SomeClass() : SOME_CONST(4),var(0){}
SomeClass::SomeClass(int i) : SOME_CONST(4),var(i){}

and why?

The technical difference between the two is that in version 1, the
constructor is declared inline whereas in version 2, you would need to use
the keyword inline to achieve that. Other than that, it all depends on how
you like to organize your code in header and implementaion files.

Personally, I put everthing into what would commonly be called header files
(my code base is heavily templated, and my compiler does not support
export). Therefore, I go with the first option. It is less verbose. Some
people still like to separate header and implementation files (which would
be included by the header file in case of templated). I think, this is
mistaken as there seems to be an underlying confusion: namely that header
files are some sort of documentation.


Best

Kai-Uwe Bux
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top