473,769 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Short version:
class MyClass
{
friend class MyOtherClass::M ySubClass; // 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::CCh arFieldMap; // 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 5125
Jim Langston wrote:
Short version:
class MyClass
{
friend class MyOtherClass::M ySubClass; // 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::M ySubClass; // Does work
};
--
Ian Collins.
Aug 21 '06 #2
"Ian Collins" <ia******@hotma il.comwrote in message
news:4k******** ****@individual .net...
Jim Langston wrote:
>Short version:
class MyClass
{
friend class MyOtherClass::M ySubClass; // 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::M ySubClass; // 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::He alth' 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::CCh arFieldMap; // 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::He alth' 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******@hotma il.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::He alth' 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******@hotma il.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******@hotma il.comwrote in message
news:4k******** ****@individual .net...
Jim Langston wrote:
>"Ian Collins" <ia******@hotma il.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
CCharFieldMa p CharFieldMap;
but that still wouldn't compile.

CCharFieldMa p 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.CharFieldM ap.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
3343
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 to turn a previously made String class that deals with char's into a templated String class that uses the template parameter C instead of char. I thought it would be fairly simple to do this exercise, but I encoutered many errors for my...
3
3952
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 remove unnecessary object copies by converting to the temp class for intermediate operations. The temp class reuses allocated space as is necessary.
1
3141
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 friend classes as "friend ClassName", which is easy to remedy by replacing "friend" with "friend class". But this poses another problem: in the file metadata.h I encounter the following code: ....
7
2383
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++ newsgroups, "Thinking In C++" to no avail. Background ----------
4
1844
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
2107
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. thanks
1
1428
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 created. But, I can't get the friend class to work. For example (this is not actually my code): template <typename Key, typename Value> class MyMap; template <typename Key, typename Value> struct MyMapNode;
2
2051
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
1737
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
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9851
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8863
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7401
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.