How keep constructor from being called till "ready"?

This is a discussion on How keep constructor from being called till "ready"? within the C++ Programming forums, part of the General Programming Boards category; I know that if you do: Code: TestClass c; TestClass c = NULL; In both cases, TestClass' empty constructor is ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    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,053
    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
    21,936
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    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, 02: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, 12:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21