Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So what i want is a class, let's call it classOne has a variable of type classTwo called child, and then classTwo has a variable named parent of type classOne. In C#, you would simply do this:
C#
class classOne {
	classTwo child;
}

class classTwo {
	classOne parent;
}


But i have tried and tried with references, pointers, and everything in between but i cannot get something like this to compile in C or C++, especially not if i have multiple, say a classThree and i want all the 3 classes to have variables of eachother. What would be the easiest way to implement this. I would appreciate high performance, but it's not super important at this stage, as i am still at the "make it work as simple as possible" stage of my application.

This is the code now:
C++
#include <stdio.h>

class classOne;
class classTwo;

class classOne {
public:
	classTwo* two;
	int a = 32;
};

class classTwo {
public:
	classOne* one;
	int b = 64;
};

int main() {
	classOne cls;
	printf("%d", cls.two->b);
	return 0;
}


But the problem is it doesn't print 64. But, if i do cls.a it prints 32.

What I have tried:

I have tried using pointers and references but could not figure it out.
Posted
Updated 26-Feb-23 12:53pm
v2

C++
#include <stdio.h>

struct classOne; //class and struct are the same thing except for
struct classTwo; //member visibility. I changed to struct to avoid
                 //these problems for the moment
struct classOne {
public:
	classTwo* two;
	int a = 32;
};

struct classTwo {
public:
	classOne* one;
	int b = 64;
};

int main() {
	classOne cls;
    cls.two = new classTwo; // pointer needs to be initialized 
	printf("%d", cls.two->b);
	return 0;
}
A class (or struct) declaration does not create an object. It creates a template for objects. You need to explicitly create both objects and somehow set the pointers. In the code above I allocated the classTwo object with new. Alternatively you can allocate it statically:
C++
int main() {
	classOne cls;
    classTwo cls2;
    cls.two = &cls2; // pointer needs to be initialized 
	printf("%d", cls.two->b);
	return 0;
}

In any case the pointer(s) needs to be initialized.

One more thing: the syntax you used int a=32; is not idiomatic. It is legal (since C++11) but it's not how typically objects are initialized.

The "classical" way is to have a constructor initialize all members. In you particular case a constructor for classOne could look like this:
C++
struct classOne {
    classOne (classTwo* the_two) : a(32), two(the_two) {};
	classTwo* two;
	int a;
};
 
Share this answer
 
v2
Comments
Nelek 26-Feb-23 14:23pm    
[Nitpicky mode on]
why did you not edit your previous answer instead of posting a second message?
[Nitpicky mode off]
Mircea Neacsu 26-Feb-23 14:26pm    
Because I'm a greenhorn when it comes to answering questions :) Thanks for nitpicking: that helps me improve
Jamie Engel 26-Feb-23 15:36pm    
It still won't print anything, at this point i am doubting both by C skills and my compiler xD
C++
class classTwo; //forward declaration

class classOne {
  classTwo *child; 
};

class classTwo {
  classOne *parent;
}; 
 
Share this answer
 
Comments
Jamie Engel 26-Feb-23 13:12pm    
It still doesn't work like i want it too. If i have a int b = 64 in classTwo and i do "classOne cls; printf("%d", cls.two->b);" it compiles but when i run it it doesn't print anything.
Mircea Neacsu 26-Feb-23 13:14pm    
Can you post the code?
Jamie Engel 26-Feb-23 14:04pm    
I improved the question with the code :)
PJ Arends 26-Feb-23 14:17pm    
cls.two has not been initialized so it is pointing to just some random memory.
Jamie Engel 26-Feb-23 14:44pm    
how do i initialize it? Do i use "new"?
As others have already written, the instances of the classes must first be created and then the pointer in each case can be stored in the other class. For the sake of symmetry, I would provide a method for each.
Usually also member variables would be kept as private as possible.

C++
class classOne;  // forward declarations
class classTwo;

class classOne {
public:
	classOne() : m_a(32), m_parent(nullptr) {};
	void setparent(classTwo& parent) { m_parent = &parent; }
//private:
	classTwo* m_parent;
	int m_a;
};

class classTwo { /* TODO */ };

// use as follows
classOne one;
classTwo two;

one.setparent(two);

std::cout << one.m_parent->m_b << "\n";
 
Share this answer
 
v3

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