Thread: General C++ Question

  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, 11:00 AM
  2. winsock - general question
    By keira in forum Windows Programming
    Replies: 1
    Last Post: 09-28-2007, 01: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-25-2002, 11:51 PM
  5. general question regarding a function parameter
    By mlupo in forum C Programming
    Replies: 7
    Last Post: 10-13-2002, 07:32 PM