Thread: instantiation of a static object within class

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    13

    instantiation of a static object within class

    Hi all, I've got a question about how to introduce other classes objects as member in another class:
    Code:
    #include "otherclass.h"
    
    Class MyClass {
    
    private:
    
    int member1;
    Otherclass oth;
    
    };
    This works until I need to pass arguments to Otherclass constructor: am I forced to declare member as a pointer and then instantiate it as new within the MyClass constructor?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you can do it on the MyClass constructor....
    Code:
    MyClass(int x) : oth(x)
    {
    }
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    mmm ok...seems the quickest solution, thank you

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Unless your other class supports a copy constructor, its more like the "only" solution. You should also be aware that you can initialize any member this way, including that member1 variable. As a matter of preference that is how I typically do any of my explicit initializations in a ctor. I reserve the alternative method of initialiizing variables from within the body of the ctor for dynamic memory allocation.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    ok, thanks for the precisation. And if I would give a copy constructor to the other class? Which other alternatives do I have?

    p.s. I can't remember why, in the copy constructor interface, object must by passed by reference (declaring (const Myclass &))

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You could write something like this:

    Example:
    Code:
    class Class1
    {
    private:
      int value;
    
    public:
      Class1() : value(-1)
      {
    
      }
    
      Class1(const Class1 &o) : value(o.value)
      {
    
      }
    
      Class1(int _value) : value(_value)
      {
    
      }
    
      Class1 &operator = (int value)
      {
        this->value = value;
      }
    };
    
    
    class Class2
    {
    private:
      Class1 member;
    
    public:
      /* NOTE: This code can't be compiled. Its just for demonstrational purposes */
    
      /* Option 1 */
      Class2(int value) : member(value)
      {
    
      }
    
      /* Option 2 */
      Class2(int value)
      {
        Class1 temp = value;
        member = temp;
      }
    
      /* Option 3 */
      Class2(int value)
      {
        member = value;
      }
    };
    Help?

    >I can't remember why, in the copy constructor interface, object must by passed by reference (declaring (const Myclass &))

    Because Stroustrup said so. Just kidding, actually it is because of the fact that it guarantees that when object A is being made, it is not going to alter the object (object B) which it is copying. Afterall, what kind of copy are you making if you are changing around the thing you are supposed to be cloning, right?

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    I should have understood more or less, I'll take notes for my next writings. thank you so much, very powerful explanation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking error with static template class members
    By cunnus88 in forum C++ Programming
    Replies: 6
    Last Post: 04-02-2009, 12:31 PM
  2. Is there a way to tell what class an object is?
    By Loduwijk in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2006, 09:20 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  5. static class problem.
    By Sebastiani in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2002, 03:27 PM