General C++ Question

This is a discussion on General C++ Question within the C++ Programming forums, part of the General Programming Boards category; Ok, there are 2 things that have always confused me in C++: 1) If I have an object (say an ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    1

    General C++ Question

    Ok, there are 2 things that have always confused me in C++:
    1) If I have an object (say an instance of myClass) that I want to be global in all of my .cpp files, how do I do that?

    2) How can I use an instance of class B in class A before the actual definition of B?
    ie:
    Code:
    some .h file
    
    class A {
    private:
      B myB;
    };
    ...
    class B{
    //definition of B
    }
    I am using Visual C++ 6

    Thanks.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: General C++ Question

    Originally posted by IncarnateRW
    1) If I have an object (say an instance of myClass) that I want to be global in all of my .cpp files, how do I do that?
    Define that variable in all cpp files (with the same name of course). Make sure that all of them but one has the keyword extern in front.

    2) How can I use an instance of class B in class A before the actual definition of B?
    I believe you could declare the class first, but I'm not 100% sure:
    Code:
    class B;
    
    class A
    {
       B MyInnerClass;
    }
    
    class B
    {
       ...
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    yes you can declare the an undefined class first...its just like a function prototype

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A general question aout programming?
    By bradt93 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-26-2008, 12:00 PM
  2. winsock - general question
    By keira in forum Windows Programming
    Replies: 1
    Last Post: 09-28-2007, 02:56 PM
  3. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM
  4. general programming compatability question
    By Metarectilinear in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-26-2002, 12:51 AM
  5. general question regarding a function parameter
    By mlupo in forum C Programming
    Replies: 7
    Last Post: 10-13-2002, 08:32 PM

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