Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone!

I am trying to learn c++ by my own. The reason being, that I actually know c# and I really like it, its very neat and beautifull but not portobale and not as fast as c++(From what I´ve researched).

So one of the "new" things Im learning are the use of pointers (I didnt used them in c# because they where 'insecure').

I have Two classes: Class A and Class B(they are both on main.cpp)

C++
class A
{
    public:
        int ClassA_variable;
};


C++
class B
{
    public:
        void printClassA_variable(A *myObject)
        {
            cout << myObject->ClassA_variable << endl;
        }
};


Class B member function is supposed to take a pointer to an object and display its variable value.

In main:

C++
int main()
{
   A *pointer = new A;
   B object2;

   pointer->ClassA_variable = 6;
   object2.printClassA_variable(pointer);

   return 0;
}

You may think this aint my code but it actually is, this is a concept that hit me and I tried to create it in a simplyfied way(simple solutions are right solutions).

So this works because class A its defined before class B, but if I write a function in class A that accepts a pointer to a B object it fails cause class B isnt defined at that moment.

¿How can I solve this?
¿What should I do if I want to separate class A & class B into 2 different .h files and use them in main?

Thanks in advanced by your answers or helpful advices.
Posted

Well, yes I believe it is your code... at least the code of a C# developer. :-)

If you have 2 classes that reference each other, a couple of techniques you likely want to learn are:
(1) use of .cpp and .h files, with the class definition in the header file and the implementation in the .cpp file. Usually, one pair of files per class.
(2) forward definitions. You will use this technique in at least one of your header files.

If you declare a forward pointer to a class, you can create a pointer to that class before the class is defined.

If you define a class's interfaces (ie. the data and methods) before those methods are implemented, you can write code that calls them. ... oh yeah, then you can write the methods.

If you define and implement a class in one place (as you did, which is the C# style), then the compiler can (and usually does) consider that all of the methods are to be 'inline'. You typically don't want this, except for accessors or very short methods with trivial to simple logic. Generally you want to use both a header and implementation file for a given class. There are exceptions to this but they are relatively rare.

Here is some sample code (which I created by hand and didn't compile, if any errors are present).

C++
// ClassA.h
class B;  // forward declaration

class A
{
public:
  A(B* b) { m_b = b; }
  void foo();
private:
  B* m_b;
};

C++
// ClassA.cpp
#include "ClassA.h"
#include "ClassB.h"

A::foo()
{ m_b->bar(); }


C++
// ClassB.h
class A;

class B
{
public:
  B(A* a) { m_a = a; }
  void bar();
private:
  A* m_a;
};

C++
// ClassB.cpp
#include "ClassB.h"
#include "ClassA.h"

void B::bar()
{
  m_a->foo();
}


This code defines two classes that can reference each other. Hopefully you know how to turn the crank the rest of the way...
 
Share this answer
 
Comments
Member 8437747 14-Jul-13 22:56pm    
Hahaha I wrote that because in some of my questions, when I wrote code in that simplified way people often asked me for the real code.

Sorry if dont catch up that fast Im young and I only have a couple days in c++.

I really appreciate your help and admire your experience.
H.Brydon 14-Jul-13 23:14pm    
The code you provided is fine. You provided just the right amount for the question.

Thanks for your compliments.
This is the technical solution. In practice this type of interdependency is best designed out.

C++
class B
{
public:
	int ClassB_variable;
    void printClassA_variable(class A *myObject);
};


class A
{
    public:
        int ClassA_variable;

        void printClassB_variable(B *BObject)
        {
            cout << BObject->ClassB_variable << endl;
        };

};

void B::printClassA_variable(A *myObject)
{
       cout << myObject->ClassA_variable << endl;
}


int _tmain(int argc, _TCHAR* argv[])
{
A object1 = {6};
B object2 = {9};
B* object3 = new B;


object1.printClassB_variable(&object2);
object2.printClassA_variable(&object1);
object3->printClassA_variable(&object1);


return 0;
}
 
Share this answer
 
v2
Comments
Member 8437747 14-Jul-13 20:52pm    
If I have the two classes on separate header files, using the keyword 'class'

as in: "void printClassA_variable(class A *myObject);" will be usefull?

or what should I write so both know of their existence?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900