Thread: How keep constructor from being called till "ready"?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    How keep constructor from being called till "ready"?

    I know that if you do:

    Code:
    TestClass c;
    TestClass c = NULL;
    In both cases, TestClass' empty constructor is called. But I want to declare that a class has that variable but not call the constructor until the owning class' constructor is called.

    Example:

    Code:
    class Owner
    {
    protected:
         //Want this but not initialized!
         TestClass t;
    
    public:
         Owner()
         {
              t = TestClass();
         }
    
         Owner(string name)
         {
              t = TestClass( name );
         }
    };
    How would I do this (i.e. keep "t" from being initialized)?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    In order to do this (and I'm not sure why you would), you would have to hold that variable as a pointer, and then allocate an object with "new" at the proper moment. Now that you'd used new, you need to use delete at some point, or use a smart pointer. Why are you trying to do this?

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by brewbuck View Post
    In order to do this (and I'm not sure why you would), you would have to hold that variable as a pointer, and then allocate an object with "new" at the proper moment. Now that you'd used new, you need to use delete at some point, or use a smart pointer. Why are you trying to do this?
    OK, I really wanted to do it without a pointer. Is there no other way?

    The reason I'm doing it is that this class is an interface into data stored in TLS and if it's constructed with an argument, it saves that to TLS otherwise it fchecks if the data is in the TLS and if not it creates the data from a configuration and stores that.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by 6tr6tr View Post
    OK, I really wanted to do it without a pointer. Is there no other way?

    The reason I'm doing it is that this class is an interface into data stored in TLS and if it's constructed with an argument, it saves that to TLS otherwise it fchecks if the data is in the TLS and if not it creates the data from a configuration and stores that.
    Perhaps a class is not really the right solution here. Perhaps just create a global SetOwner() function which does this "stuff" depending on whether it was passed an argument or not. A class really, really wants to construct all of its members, so doing goofy stuff to try to avoid that behavior is probably a sign that a class is not the right design.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Surely there is an better and easier way to do this. Why not simply remove the ctr for TestClass from the default ctor for Owner? Wouldn't that accomplish the same. Thats about all I got for trying to avoid pointer usage... Why not use pointers though? It makes your unusual need more plausable

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Use a pointer. Don't be scared. This is what pointers are made for.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But I want to declare that a class has that variable but not call the constructor until the owning class' constructor is called.
    I do not see the problem in the first place. Until the Owner constructor is invoked, the TestClass constructor for the member variable t will not be invoked since that member object cannot exist without the Owner object.

    In other words, to keep t from being initialised, keep its Owner object from being initialised.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you can, you can cut out the initialization code from the constructor and put it into a member function called Init or something. Do whatever you need to do and then call Init.
    It's the easiest solution if you can do it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read characters till white space
    By ashok449 in forum C Programming
    Replies: 2
    Last Post: 03-08-2008, 03:56 AM
  2. this returned destructor called...
    By Carlos in forum C++ Programming
    Replies: 54
    Last Post: 09-30-2003, 03:31 AM
  3. config files for exe's that are called with a batch
    By stormbringer in forum Windows Programming
    Replies: 2
    Last Post: 06-24-2003, 09:05 AM
  4. Finding the line and file in which a function was called
    By roktsyntst in forum C++ Programming
    Replies: 2
    Last Post: 04-20-2003, 10:30 AM
  5. What is this program called?
    By DarkViper in forum Linux Programming
    Replies: 6
    Last Post: 12-05-2002, 01:27 PM