473,378 Members | 1,500 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Call constructor in another

hi,

I got a class have 2 constructors:

static int g_idx_counter = 0;

Expand|Select|Wrap|Line Numbers
  1. Object::Object():{
  2. counter = g_idx_counter++;
  3. created_at = last_used = time(NULL);
  4. destroyed = false;
  5. id = -1;
  6. }
  7.  
  8. Object::Object(int _id){
  9. Object();
  10. id = _id;
  11. }
  12.  
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
Apr 29 '07 #1
7 2314
Blair Craft wrote:
hi,

I got a class have 2 constructors:

static int g_idx_counter = 0;

Expand|Select|Wrap|Line Numbers
  1. Object::Object():{
  2.    counter = g_idx_counter++;
  3.    created_at = last_used = time(NULL);
  4.    destroyed = false;
  5.    id = -1;
  6. }
  7. Object::Object(int _id){
  8.     Object();
Expand|Select|Wrap|Line Numbers
  1.  
  2. The line Object() creates a temporary object of type Object and initializes
  3. it using the constructor Object::Object(). It does not call the constructor
  4. Object::Object() for the object currently being initialized by
  5. Object::Object(int).
  6.  
  7.         
  8.                     id = _id;
  9. }
  10.  
  11.  
>
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

Apr 29 '07 #2
uh-huh, it's a common mistake for beginners

Apr 29 '07 #3
On Apr 28, 12:58 am, Blair Craft <lai...@square-enix.net.cnwrote:
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;

Expand|Select|Wrap|Line Numbers
  1. Object::Object():{
  2.    counter = g_idx_counter++;
  3.    created_at = last_used = time(NULL);
  4.    destroyed = false;
  5.    id = -1;
  6. }
Expand|Select|Wrap|Line Numbers
  1.  
  2. Use init lists.
  3. // type int is a wild guess, whats type is id?
  4.  
  5. Object::Object( int n = -1 )
  6. : counter(++g_idx_counter),
  7. created_at(time(NULL)),
  8. last_used(time(NULL)),
  9. destroyed(false),
  10. id(n)
  11. {
  12. }
  13.  
  14.         
  15.                 >
  16. Object::Object(int _id){
  17.     Object();
  18.     id = _id;}
  19.  
  20. The above ctor is not needed, Object() is a local temporary, a
  21. competant compiler would probably optimize it away with no observeable
  22. affect to the result. The above only assigns id.
  23.  
  24.         
  25.                 >
  26.  
  27.  
>
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.
Apr 29 '07 #4
On Apr 29, 8:27 am, Salt_Peter <pj_h...@yahoo.comwrote:
On Apr 28, 12:58 am, Blair Craft <lai...@square-enix.net.cnwrote:
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;
Expand|Select|Wrap|Line Numbers
  1.  Object::Object():{
  2.     counter = g_idx_counter++;
  3.     created_at = last_used = time(NULL);
  4.     destroyed = false;
  5.     id = -1;
Expand|Select|Wrap|Line Numbers
  1.         
  2.                  }
  •  
  • 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)
  • {
  • }
  •         
  •                  Object::Object(int _id){
  •      Object();
  •      id = _id;}
  •  
  • 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.
  •         
  •  
  •  
  •  
  • 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. 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.

    Apr 29 '07 #5
    On Apr 28, 8:58 am, Blair Craft <lai...@square-enix.net.cnwrote:
    hi,

    I got a class have 2 constructors:

    static int g_idx_counter = 0;

    Expand|Select|Wrap|Line Numbers
    1. Object::Object():{
    2.    counter = g_idx_counter++;
    3.    created_at = last_used = time(NULL);
    4.    destroyed = false;
    5.    id = -1;
    6. }
    7. Object::Object(int _id){
    8.     Object();
    9.     id = _id;}
    10.  

    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;
    }

    Apr 29 '07 #6
    On Apr 29, 10:02 am, terminator <farid.mehr...@gmail.comwrote:
    On Apr 28, 8:58 am, Blair Craft <lai...@square-enix.net.cnwrote:
    hi,
    I got a class have 2 constructors:
    static int g_idx_counter = 0;
    Expand|Select|Wrap|Line Numbers
    1.  Object::Object():{
    2.     counter = g_idx_counter++;
    3.     created_at = last_used = time(NULL);
    4.     destroyed = false;
    5.     id = -1;
    Expand|Select|Wrap|Line Numbers
    1.         
    2.                  }
  •  
  •         
  •                  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;

    }; A private initializer member function is a good solution. The above
    ctor is a copy ctor.
    so calling the initializer function here
    >
    Object::Object(int _id){
    build();
    id = _id;

    }

    Apr 29 '07 #7
    On Apr 29, 10:02 am, terminator <farid.mehr...@gmail.comwrote:
    On Apr 28, 8:58 am, Blair Craft <lai...@square-enix.net.cnwrote:
    hi,
    I got a class have 2 constructors:
    static int g_idx_counter = 0;
    Expand|Select|Wrap|Line Numbers
    1.  Object::Object():{
    2.     counter = g_idx_counter++;
    3.     created_at = last_used = time(NULL);
    4.     destroyed = false;
    5.     id = -1;
    Expand|Select|Wrap|Line Numbers
    1.         
    2.                  }
  •  
  •         
  •                  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;

    }; 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.
    >
    Object::Object(int _id){
    build();
    id = _id;

    }

    Apr 29 '07 #8

    This thread has been closed and replies have been disabled. Please start a new discussion.

    Similar topics

    3
    by: S³awek | last post by:
    Can one constructor of an object call another constructor of the same class? class foo { foo(float f, int i) // a "full" constructor { ... } foo(int i) // a "simplified" constructor {
    23
    by: Fabian Müller | last post by:
    Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
    26
    by: Paul | last post by:
    public class A { public A () { // here I would like to call the second version of _ctor, how to accomplish this ? } public A (int a, int b, int c) {
    5
    by: Wilfried Mestdagh | last post by:
    Hi, I want to overload a constructor of a class. But I want to call the other one from the second if called. I explain with code because of my english: public class XMLConfig { public...
    9
    by: Andy | last post by:
    Hi, is it in C++ possible to call a constructor from another constructor? In Java you can do the following: public class MyClass { public MyClass() { this(10);
    18
    by: cmk128 | last post by:
    hi 1 #include <stdio.h> 2 3 class A{ 4 private: 5 int x; 6 public: 7 A(int x){ 8 this->x;
    3
    by: mike.arsenault | last post by:
    Hello I need some help from anyone that can provide it. Below is a function inside a template collection class that I'm writing. I have a TYPE * that points to an allocated memory location, and...
    7
    by: cppquester | last post by:
    What does this code do? #include <iostream> class A { public: A() { std::cout << "A::A()" << std::endl;} };
    5
    by: Warren Tang | last post by:
    Hello, Does C++ allow a constructor to call another constructor of the same class? (I am focusing on the language abilities. I know the C# language allow this.) The following sample can...
    6
    by: howa | last post by:
    Consider example: Animal = function(age) { this.age = age; }; Animal.prototype.sleep = function() { alert("Animal Sleeping..."); };
    1
    by: CloudSolutions | last post by:
    Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
    0
    isladogs
    by: isladogs | last post by:
    The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
    0
    by: ryjfgjl | last post by:
    In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
    0
    by: taylorcarr | last post by:
    A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
    0
    by: Charles Arthur | last post by:
    How do i turn on java script on a villaon, callus and itel keypad mobile phone
    0
    by: aa123db | last post by:
    Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
    1
    by: nemocccc | last post by:
    hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
    1
    by: Sonnysonu | last post by:
    This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
    0
    by: Hystou | last post by:
    There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

    By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

    To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.