You are here:

C++/Object References in C++

Advertisement


Question
In this case, should all of the classes be declared within the same headerfile?  Because it seams like if seed.h included pear.h, and pear.h included seed.h (being that you have to have the declarations to create an object of a given type)... seems like a circular reference?

Anyway, your response gave me a good idea of how to make this work... thank you very much.  If you get a moment... can you tell me how to establish a variable as global?

-- Dave
-------------------------
Followup To
Question -
I am using MSVC++ 6.0 and while this may seem some bit basic, I just need to make sure that I make this work appropriately.

I am a veterin programmer in most aspects, but I am still getting around with C++.  Working with smaller applications is pretty easy, but I am a little stuck with my existing project because I am working with many classes within many documents this time.

My problem specifically occures because all of my objects include many references to eachother, so I will make a short example of it.

I have an object that I wish to be loaded as a static object (a global in most languages), which can be easily referenced from all classes.  I want this global object to be able to load new instances of any object within my object model, and define relationships within a heierarchial format (where every object functions as a basic node, which can contain any other type of object).  I have figured most of it out, but first off, I am having a lot of difficulty making my innitial object global (I have instead been declaring it within the <project>App header file to insure that only one instance is loaded... then I have to create a function which actually loads each type of object and assigns a reference to the 1 instance of my Application Class.

Further still, it seems like (for example) I cannot reference an object until after it has been declared... for example:

class pear : public someBaseClass
{
public:
 someBaseClass* GetASeed()
 {
    // seed derived from someBaseClass
    someBaseClass *pNewClass = new seed();
    return(pNewClass);
 }
};

class seed : public someBaseClass
{
public:
 someBaseClass* getAPear()
 {
     someBaseClass *pNewClass = new pear();
     return(pNewClass);
 }
};

not sure if I did the pointers correctly (as I often have to debug that part, but can always get it right eventually) but the problem I am having is I get an error because seed is defined after pear, and therefore cannot be used.  In the case of my application, many of my methods are returning one of 2 or 3 dozen objects which may be declared before or after it.

When I try to break them all into seperate files and have each include the rest... I get all sorts of problems.  I know that there has to be a way... I am just a little confused about it.  Any help would be appreciated... THANKS!

- Dave
Answer -
Dave, Thank you for your question.

What you need is to separate declarations from definitions.

Here is an example that compiles successfully:

// In Include files:

class someBaseClass
  {
public:
  int x;
  // General object constructor
  someBaseClass();
  };

class pear : public someBaseClass
{
public:
someBaseClass* GetASeed();
};

class seed : public someBaseClass
{
public:
someBaseClass* getAPear();
};  

// In Implementation (source) files:

// General object constructor
someBaseClass::someBaseClass()
  {
  }

someBaseClass* pear::GetASeed()
{
   // seed derived from someBaseClass
   someBaseClass *pNewClass = new seed();
   return(pNewClass);
}

someBaseClass* seed::getAPear()
{
   someBaseClass *pNewClass = new pear();
   return(pNewClass);
}

David Spector


Answer
Dave,

Include files are for just for the convenience of the programmer. They are processed only by the C Preprocessor; the C++ compiler simply sees their content, in order of inclusion. You are free to use them as you wish. Typical programming convention is to put potentially reusible declarations in Include files, and to exclude executable source code (except for template-based libraries such as ATL and STL).

You can avoid circular inclusion of Include files by surrounding the text of each with a conditional preprocessor statement. This example is from the STL 'Vector' file:

#ifndef _VECTOR_
#define _VECTOR_
... content ...
#endif /* _VECTOR_ */


To declare a global variable:

Include file:

extern seed * CurSeed; // The current seed being planted

The main source file:

seed * CurSeed; // The current seed being planted

David

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


David Spector

Expertise

Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.

Experience

I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Publications
Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

Education/Credentials
ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2016 About.com. All rights reserved.