Share via

class initialization

josh BAUER 166 Reputation points
2020-11-29T17:54:36.413+00:00

Is it possible to initialize class with class declared later?

class myClass1{
public:
  myClass1(myClass2 temp){
  }
}
class myClass2{
public:
  myClass2(myClass1 temp){
  }
}


Can I initialize one class with other if they are in mixed order?
I cannot position them in sequence because 
I would like all classes to depend upon them each other.

Can i do it?
Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

0 comments No comments

Answer accepted by question author

Viorel 127K Reputation points
2020-11-29T18:02:23.84+00:00

Try something like this:

class myClass2;

class myClass1
{
public:
    myClass1( myClass2 temp );
};

class myClass2
{
public:
    myClass2( myClass1 temp )
    {
        // . . .
    }
};

myClass1::myClass1( myClass2 temp )
{
    // . . .
}

Also consider passing by references — ‘myClass&’ or ‘const myClass&’ — instead of values.

You will probably need some other constructors too.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.