Call constructor in another

B

Blair Craft

hi,

I got a class have 2 constructors:

static int g_idx_counter = 0;

Code:
Object::Object():{
   counter = g_idx_counter++;
   created_at = last_used = time(NULL);
   destroyed = false;
   id = -1;
}

Object::Object(int _id){
    Object();
    id = _id;
}

when I use:

Object *o = new Object();

everything is OK, but when using the second constructor:

Object *o = new Object(32);

member variable "counter" will remain untouched, I did a gdb trace,
Object::Object() was invoked and inside that function "counter" was
initialized, when function returns "counter" went back to 0
again. Behavior is like a local variable inside a code chunk, but here
counter is a class member variable, anyone can shed some light?

thanks
 
K

Kai-Uwe Bux

Blair said:
hi,

I got a class have 2 constructors:

static int g_idx_counter = 0;

Code:
Object::Object():{
counter = g_idx_counter++;
created_at = last_used = time(NULL);
destroyed = false;
id = -1;
}

Object::Object(int _id){
Object();[/QUOTE]

The line Object() creates a temporary object of type Object and initializes
it using the constructor Object::Object(). It does not call the constructor
Object::Object() for the object currently being initialized by
Object::Object(int).
[QUOTE]
id = _id;
}

when I use:

Object *o = new Object();

everything is OK, but when using the second constructor:

Object *o = new Object(32);

member variable "counter" will remain untouched, I did a gdb trace,
Object::Object() was invoked and inside that function "counter" was
initialized, when function returns "counter" went back to 0
again. Behavior is like a local variable inside a code chunk, but here
counter is a class member variable, anyone can shed some light?

Constructors do not work like ordinary member functions. That's why they
are "special".


Best

Kai-Uwe Bux
 
S

Salt_Peter

hi,

I got a class have 2 constructors:

You actually have 3, if you don't declare a copy ctor, the compiler
generates one for you ( thats a hint ).
static int g_idx_counter = 0;

Code:
Object::Object():{
counter = g_idx_counter++;
created_at = last_used = time(NULL);
destroyed = false;
id = -1;

}[/QUOTE]

Use init lists.
// type int is a wild guess, whats type is id?

Object::Object( int n = -1 )
                        : counter(++g_idx_counter),
                          created_at(time(NULL)),
                          last_used(time(NULL)),
                          destroyed(false),
                          id(n)
{
}
[QUOTE]
Object::Object(int _id){
Object();
id = _id;}[/QUOTE]

The above ctor is not needed, Object() is a local temporary, a
competant compiler would probably optimize it away with no observeable
affect to the result. The above only assigns id.
[QUOTE]

when I use:

Object *o = new Object();

Object* p_obj = new Object;
everything is OK, but when using the second constructor:

Object *o = new Object(32);

member variable "counter" will remain untouched, I did a gdb trace,
Object::Object() was invoked and inside that function "counter" was
initialized, when function returns "counter" went back to 0
again. Behavior is like a local variable inside a code chunk, but here
counter is a class member variable, anyone can shed some light?

thanks

What you saw is expected, Why should member counter be modified by a
local Object() in any way?
Next time, please reproduce a basic reconstruction of the Object
class, since the order of its member declarations can affect the
inititialization list. Its also a pain to 'assume' what id might be.
 
M

Mitesh

I got a class have 2 constructors:

You actually have 3, if you don't declare a copy ctor, the compiler
generates one for you ( thats a hint ).


static int g_idx_counter = 0;
Code:
Object::Object():{
counter = g_idx_counter++;
created_at = last_used = time(NULL);
destroyed = false;
id = -1;[/QUOTE]
[QUOTE]
}[/QUOTE]

Use init lists.
// type int is a wild guess, whats type is id?

Object::Object( int n = -1 )
: counter(++g_idx_counter),
created_at(time(NULL)),
last_used(time(NULL)),
destroyed(false),
id(n)
{

}
[QUOTE]
Object::Object(int _id){
Object();
id = _id;}[/QUOTE]

The above ctor is not needed, Object() is a local temporary, a
competant compiler would probably optimize it away with no observeable
affect to the result. The above only assigns id.


[QUOTE]
when I use:
Object *o = new Object();

Object* p_obj = new Object;


everything is OK, but when using the second constructor:
Object *o = new Object(32);
member variable "counter" will remain untouched, I did a gdb trace,
Object::Object() was invoked and inside that function "counter" was
initialized, when function returns "counter" went back to 0
again. Behavior is like a local variable inside a code chunk, but here
counter is a class member variable, anyone can shed some light?

What you saw is expected, Why should member counter be modified by a
local Object() in any way?
Next time, please reproduce a basic reconstruction of the Object
class, since the order of its member declarations can affect the
inititialization list. Its also a pain to 'assume' what id might be.

One approach you can take is use a private initialization function and
call it any number of times. However one bad thing is that you won't
be able to use the member initialization list.
 
T

terminator

hi,

I got a class have 2 constructors:

static int g_idx_counter = 0;

Code:
Object::Object():{
counter = g_idx_counter++;
created_at = last_used = time(NULL);
destroyed = false;
id = -1;

}

Object::Object(int _id){
Object();
id = _id;}

when I use:

Object *o = new Object();

everything is OK, but when using the second constructor:

Object *o = new Object(32);

member variable "counter" will remain untouched, I did a gdb trace,
Object::Object() was invoked and inside that function "counter" was
initialized, when function returns "counter" went back to 0
again. Behavior is like a local variable inside a code chunk, but here
counter is a class member variable, anyone can shed some light?

thanks

put shared instructions in a member function and call it in both
ctors:

void Object::build(){
counter = g_idx_counter++;
created_at = last_used = time(NULL);
destroyed = false;
id = -1;
};

Object::Object(){
build();
};

Object::Object(const Object& ob){
build();
id=ob.id;
};

Object::Object(int _id){
build();
id = _id;
}
 
S

Salt_Peter

I got a class have 2 constructors:
static int g_idx_counter = 0;
Code:
Object::Object():{
counter = g_idx_counter++;
created_at = last_used = time(NULL);
destroyed = false;
id = -1; [QUOTE]
}[/QUOTE]

Object::Object(int _id){
Object();
id = _id;} [QUOTE]

when I use:
Object *o = new Object();
everything is OK, but when using the second constructor:
Object *o = new Object(32);
member variable "counter" will remain untouched, I did a gdb trace,
Object::Object() was invoked and inside that function "counter" was
initialized, when function returns "counter" went back to 0
again. Behavior is like a local variable inside a code chunk, but here
counter is a class member variable, anyone can shed some light?

put shared instructions in a member function and call it in both
ctors:

void Object::build(){
counter = g_idx_counter++;
created_at = last_used = time(NULL);
destroyed = false;
id = -1;

};

Object::Object(){
build();

};

Object::Object(const Object& ob){
build();
id=ob.id;

};[/QUOTE]

A private initializer member function is a good solution. The above
ctor is a copy ctor.
so calling the initializer function here
 
S

Salt_Peter

I got a class have 2 constructors:
static int g_idx_counter = 0;
Code:
Object::Object():{
counter = g_idx_counter++;
created_at = last_used = time(NULL);
destroyed = false;
id = -1; [QUOTE]
}[/QUOTE]

Object::Object(int _id){
Object();
id = _id;} [QUOTE]

when I use:
Object *o = new Object();
everything is OK, but when using the second constructor:
Object *o = new Object(32);
member variable "counter" will remain untouched, I did a gdb trace,
Object::Object() was invoked and inside that function "counter" was
initialized, when function returns "counter" went back to 0
again. Behavior is like a local variable inside a code chunk, but here
counter is a class member variable, anyone can shed some light?

put shared instructions in a member function and call it in both
ctors:

void Object::build(){
counter = g_idx_counter++;
created_at = last_used = time(NULL);
destroyed = false;
id = -1;

};

Object::Object(){
build();

};

Object::Object(const Object& ob){
build();
id=ob.id;

};[/QUOTE]

Thats a copy ctor. Assuming that Objects are indeed copyable, that may
not be what the OP wants.
There is nothing wrong with using a private member function as an
initializer otherwise.
 

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,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top