473,395 Members | 2,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to make a sub class a friend in another class?

Short version:
class MyClass
{
friend class MyOtherClass::MySubClass; // Doesn't work
};

class MyOtherClass
{
class MySubClass
{
// Need to access private members of MyClass here
} MySubInstance;
};

Here is something more close to what I am trying to do showing the problem:

#include <iostream>
#include <string>

class COffsetMap
{
public:
void SetBase( void* Base ) { }
void SetOffset( int& Var ) { }
};

class CHealth
{
public:
friend class CCharacter::CCharFieldMap; // Doesn't work
private:
int Mana_;
int ManaMax_;
};

class CCharacter
{
public:
class CCharFieldMap: public COffsetMap
{
public:
void SetMap( CCharacter* Base )
{
SetBase( Base );
SetOffset( Base->Version );
SetOffset( Base->Health.Mana_ ); // Can't access private
SetOffset( Base->Health.ManaMax_ ); // Can't access private
}
} CharFieldMap;

private:
int Version;
CHealth Health;

};

int main()
{
CCharacter Char;

std::cout << "Press Return";
std::string wait;
std::getline( std::cin, wait );
return 0;
}
I've tried various ways around this to no avail. Is this possible?
Aug 21 '06 #1
6 5103
Jim Langston wrote:
Short version:
class MyClass
{
friend class MyOtherClass::MySubClass; // Doesn't work
};

class MyOtherClass
{
class MySubClass
{
// Need to access private members of MyClass here
} MySubInstance;
};
Do it the other way round:

class MyOtherClass
{
friend class MyClass;

class MySubClass
{
// Need to access private members of MyClass here
} MySubInstance;
};

class MyClass
{
friend class MyOtherClass::MySubClass; // Does work
};
--
Ian Collins.
Aug 21 '06 #2
"Ian Collins" <ia******@hotmail.comwrote in message
news:4k************@individual.net...
Jim Langston wrote:
>Short version:
class MyClass
{
friend class MyOtherClass::MySubClass; // Doesn't work
};

class MyOtherClass
{
class MySubClass
{
// Need to access private members of MyClass here
} MySubInstance;
};
Do it the other way round:

class MyOtherClass
{
friend class MyClass;

class MySubClass
{
// Need to access private members of MyClass here
} MySubInstance;
};

class MyClass
{
friend class MyOtherClass::MySubClass; // Does work
};
That doesn't work, because (as I forgot to show in my short version)
MyOtherClass has a private instance of MyClass in it.

If I declare MyClass after MyOtherClass than I get the error
error C2079: 'CCharacter::Health' uses undefined class 'CHealth'
even if I do
class MyClass;

Even if I make the instance a pointer declartion with
MyClass* MyInstance;
then MySubClass gets errors when it attempts to access the variables:
error C2027: use of undefined type 'CHealth'
see declaration of 'CHealth'

I even tried taking the class definition of MySubClass out of MyOtherClass
but then I get the problem that MySubClass needs to see the definition of
MyClass to access the variables, but as you seem to indicate, MyClass also
needs to see the definition of MyOtherClass first.

ug.

Can you get this to compile by any manipulations?

#include <iostream>
#include <string>

class COffsetMap
{
public:
void SetBase( void* Base ) { }
void SetOffset( int& Var ) { }
};

class CCharacter
{
public:
class CCharFieldMap: public COffsetMap
{
public:
void SetMap( CCharacter* Base )
{
SetBase( Base );
SetOffset( Base->Version );
SetOffset( Base->Health->Mana_ );
SetOffset( Base->Health->ManaMax_ );
}
} CharFieldMap;

private:
int Version;
CHealth* Health;

};

class CHealth
{
public:
friend class CCharacter::CCharFieldMap; // Doesn't work
private:
int Mana_;
int ManaMax_;
};

int main()
{
CCharacter Char;
Char.SetMap( &Char );

std::cout << "Press Return";
std::string wait;
std::getline( std::cin, wait );
return 0;
}


Aug 21 '06 #3
Jim Langston wrote:
>
That doesn't work, because (as I forgot to show in my short version)
MyOtherClass has a private instance of MyClass in it.

If I declare MyClass after MyOtherClass than I get the error
error C2079: 'CCharacter::Health' uses undefined class 'CHealth'
even if I do
class MyClass;

Even if I make the instance a pointer declartion with
MyClass* MyInstance;
then MySubClass gets errors when it attempts to access the variables:
error C2027: use of undefined type 'CHealth'
see declaration of 'CHealth'

I even tried taking the class definition of MySubClass out of MyOtherClass
but then I get the problem that MySubClass needs to see the definition of
MyClass to access the variables, but as you seem to indicate, MyClass also
needs to see the definition of MyOtherClass first.

ug.

Can you get this to compile by any manipulations?

#include <iostream>
#include <string>

class COffsetMap
{
public:
void SetBase( void* Base ) { }
void SetOffset( int& Var ) { }
};

class CCharacter
{
public:
class CCharFieldMap: public COffsetMap
{
public:
void SetMap( CCharacter* Base )
{
SetBase( Base );
SetOffset( Base->Version );
SetOffset( Base->Health->Mana_ );
SetOffset( Base->Health->ManaMax_ );
}
} CharFieldMap;

private:
int Version;
CHealth* Health;

};
Why not untangle the dependencies and extract CCharFieldMap from its
containing class?

--
Ian Collins.
Aug 21 '06 #4
"Ian Collins" <ia******@hotmail.comwrote in message
news:4k************@individual.net...
Jim Langston wrote:
>>
That doesn't work, because (as I forgot to show in my short version)
MyOtherClass has a private instance of MyClass in it.

If I declare MyClass after MyOtherClass than I get the error
error C2079: 'CCharacter::Health' uses undefined class 'CHealth'
even if I do
class MyClass;

Even if I make the instance a pointer declartion with
MyClass* MyInstance;
then MySubClass gets errors when it attempts to access the variables:
error C2027: use of undefined type 'CHealth'
see declaration of 'CHealth'

I even tried taking the class definition of MySubClass out of
MyOtherClass
but then I get the problem that MySubClass needs to see the definition of
MyClass to access the variables, but as you seem to indicate, MyClass
also
needs to see the definition of MyOtherClass first.

ug.

Can you get this to compile by any manipulations?

#include <iostream>
#include <string>

class COffsetMap
{
public:
void SetBase( void* Base ) { }
void SetOffset( int& Var ) { }
};

class CCharacter
{
public:
class CCharFieldMap: public COffsetMap
{
public:
void SetMap( CCharacter* Base )
{
SetBase( Base );
SetOffset( Base->Version );
SetOffset( Base->Health->Mana_ );
SetOffset( Base->Health->ManaMax_ );
}
} CharFieldMap;

private:
int Version;
CHealth* Health;

};
Why not untangle the dependencies and extract CCharFieldMap from its
containing class?
Tried that too. Couldn't get it to work either.

I even took the declaration/definition of CCharFieldMap out of CCharacter,
then in CCharacter I only do
CCharFieldMap CharFieldMap;
but that still wouldn't compile.

CCharFieldMap needs to be in CCharacter for usage. Otherwise it will
complicate usage quite a bit.
Aug 21 '06 #5
Jim Langston wrote:
"Ian Collins" <ia******@hotmail.comwrote in message
>>
Why not untangle the dependencies and extract CCharFieldMap from its
containing class?


Tried that too. Couldn't get it to work either.

I even took the declaration/definition of CCharFieldMap out of CCharacter,
then in CCharacter I only do
CCharFieldMap CharFieldMap;
but that still wouldn't compile.

CCharFieldMap needs to be in CCharacter for usage. Otherwise it will
complicate usage quite a bit.
It can still be a member, even if it isn't a nested class.

If you can't get it to compile in one file, give each class its own
header and source file and use appropriate forward declarations in the
headers. If that doesn't work, your design is too tangled and will have
to change.

--
Ian Collins.
Aug 21 '06 #6
"Ian Collins" <ia******@hotmail.comwrote in message
news:4k************@individual.net...
Jim Langston wrote:
>"Ian Collins" <ia******@hotmail.comwrote in message
>>>
Why not untangle the dependencies and extract CCharFieldMap from its
containing class?


Tried that too. Couldn't get it to work either.

I even took the declaration/definition of CCharFieldMap out of
CCharacter,
then in CCharacter I only do
CCharFieldMap CharFieldMap;
but that still wouldn't compile.

CCharFieldMap needs to be in CCharacter for usage. Otherwise it will
complicate usage quite a bit.
It can still be a member, even if it isn't a nested class.

If you can't get it to compile in one file, give each class its own
header and source file and use appropriate forward declarations in the
headers. If that doesn't work, your design is too tangled and will have
to change.
Well, I finally got it to compile. It's just very tricky in that things
have to be done in an exact order. Makes it a little difficult for the
user.

#include <iostream>
#include <string>

class COffsetMap
{
public:
void SetBase( void* Base ) { }
void SetOffset( int& Var ) { }
};

class CHealth
{
public:
friend class CCharFieldMap;
private:
int Mana_;
int ManaMax_;
};

class CCharacter;

class CCharFieldMap: public COffsetMap
{
public:
void SetMap( CCharacter* Base );
};

class CCharacter
{
public:
friend class CCharFieldMap;
CCharFieldMap CharFieldMap;

private:
int Version;
CHealth Health;

};

void CCharFieldMap::SetMap( CCharacter* Base )
{
SetBase( Base );
SetOffset( Base->Version );
SetOffset( Base->Health.Mana_ );
SetOffset( Base->Health.ManaMax_ );
}

int main()
{
CCharacter Char;
Char.CharFieldMap.SetMap( &Char );

std::cout << "Press Return";
std::string wait;
std::getline( std::cin, wait );
return 0;
}
Aug 22 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
3
by: Ryan Mitchley | last post by:
Hi I have a rather complicated class for performing matrix operations using complex arithmetic. I have written two classes, CComplexMatrix and CComplexMatrixTemp, and have been attempting to...
1
by: Alex Borghgraef | last post by:
Hi all, I'm trying to get the Limp (Large Image Manipulation Program) library to compile using gcc 3.2, and I've encountered some problems. One is that the library systematically refers to...
7
by: Thomas Matthews | last post by:
Hi, I am converting my table and record classes into templates. My issue is the syntax of declaring a friend class within the template. I have searched the C++ FAQ Lite (web), the C++...
4
by: Nataraj M | last post by:
Hi, I just don't want anybody derive a class from my class. For example: /////////////////////// //MY CODE class MyClass { .... }; ///////////////////////
21
by: anddos | last post by:
i am just wondering what is the friend in a class , ive got a book about c++ but it dosent explain deep enough . anyone care to make a example with a few notes on what the friend can benifit from....
1
by: Joe Carner via .NET 247 | last post by:
First time posting, thanks in advance for any help you can give me. Basically I am trying to a class that i want to be able to access the private data members of another class, both of which i...
2
by: freegnu | last post by:
how to declare a friend function that can access two class it will look like the following class A { private: int i; public: A(){} ~A(){} friend void call(A &a, B &b);
3
by: tonvandenheuvel | last post by:
Hi all, the following code does not compile in gcc 4.0.2: 3 class Outer 4 { 5 class B; 6 7 template<typename T>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.