-1

I have a class declared in a .cpp file, and the structure written in a .h file, in order to use the class in other .cpp files

Example :

    //class1.cpp                    //class1.h
    class class1                    class class1
    {                               {
     private:                        private:
        double X;                       double X;
        ...                             ...
     public:                         public:
        double getX(){return X;}        double getX();
        ...                             ...
    };                              };

Class2 will include "class1.h", thus allowing me to use class1, but only within new class, class2.

What I want :

    include "class1.h"

    class1 c;

    class class2{/* ... */};

Is it possible, and if so how would one go about, to declare a global object of type class1 in the class2.cpp?

1 Answers1

2

Your class1.cpp is the problem.

It should look more like this:

double class1::getX(){return X;}

You don't need that other stuff in there.

As for making c only usable in class2, you could either make your current c static like this:

static class1 c;

This causes it to only be visible in that file. Or, you could make c a static variable inside class2. class2.h:

class class2{
private:
    static class1 c;
}

class2.cpp:

class1 class2::c;

This is where you initialize it. (100% necessary)

http://www.cplusplus.com/doc/tutorial/classes/

http://www.learncpp.com/cpp-tutorial/811-static-member-variables/

DeadTomGC
  • 101
  • 4
  • Declaring a member of type class1 in the class2, isn't my problem. What I am more interested in, is using a global object class1 independently of anything else. The static attribute doesn't help me. – Galax27 Jul 05 '14 at 22:53