Thread: using a class as a data member before you initialize the class?

  1. #1
    class question
    Guest

    using a class as a data member before you initialize the class?

    Just say I have two classes set up like this:

    class One
    {
    public:
    int x;
    Two two;
    };

    class Two
    {
    public:
    Two()
    {
    One one;
    one.x = 5;
    }
    };

    It won't compile because I can't access a "Two" type before I initialize it.

    What should I do?

  2. #2
    class question
    Guest
    putting it in code

    Code:
    class One
    {
    public:
         int x;
         Two two;
    };
    
    class Two
    {
    public:
         Two()
         {
              One one;
              one.x = 5;
         }
    };

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    I have a question on top of that, how can you get all the
    function of a certain class to use the same instance of another
    class?

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: using a class as a data member before you initialize the class?

    Originally posted by class question
    Just say I have two classes set up like this:

    class One
    {
    public:
    int x;
    Two two;
    };

    class Two
    {
    public:
    Two()
    {
    One one;
    one.x = 5;
    }
    };

    It won't compile because I can't access a "Two" type before I initialize it.

    What should I do?
    You can always prototype the second class before the first one. I.E.

    Code:
    class Two; // Prototype
    
    class One 
    {
      // blah
      // blah
    };
    However looking over your code I think you are going about it wrong. In your Two class constructor you are making a temporary instance of the first class then assigning x the value of 5. Then what happens? That instance goes out of scope and is never heard from again. Re-think what you are trying to do.

    @Travis Dane

    Can you reword your question? Do you mean inheritance? I'm not quite sure what you are asking sorry.

  5. #5
    class question
    Guest

    Smile

    Hi, thanks MrWizard. My classes in the question were more of an example.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    I have a class called ABC and a class called DEF wich is a child
    of ABC, now i want to use the same object of the class DEF in
    ABC's functions.

    [edit]

    I could solve this by creating a global object of DEF but that's
    what im trying to avoid

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by class question
    putting it in code

    Code:
    class One
    {
    public:
         int x;
         Two two;
    };
    
    class Two
    {
    public:
         Two()
         {
              One one;
              one.x = 5;
         }
    };
    If that worked, it would lead to infinite recursion. Do you see why C++ has its rules?
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. How do I base size of arrays on annother number?
    By Dual-Catfish in forum C++ Programming
    Replies: 15
    Last Post: 09-25-2001, 01:31 PM