473,400 Members | 2,163 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,400 software developers and data experts.

Static function of some class as a friend of other class

I want static functions of classes Uam and Bar to be friends of class
Foo.

There is no problem with Uam::uam().
But class Bar has a member of Foo type: member Bar::m_foo, so one can't
put Bar definition before Foo definition.

As a result, a compiler rejects
friend void Bar::bar(); // in class Foo

Is there any solution for this problem?

------ foo.cpp ------
class Uam
{
static void uam() {}
};

class Bar;

class Foo
{
friend void Uam::uam();
friend void Bar::bar();
};

class Bar
{
Foo m_foo;
static void bar() {}
};
int main()
{
return 0;
}

---------------------

------ Compilation ------
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for
80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

foo.cpp
foo.cpp(11) : error C2027: use of undefined type 'Bar'
foo.cpp(6) : see declaration of 'Bar'
-------------------------

Thanks,

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jun 6 '06 #1
1 1875
Alex Vinokur wrote:
I want static functions of classes Uam and Bar to be friends of class
Foo.

There is no problem with Uam::uam().
But class Bar has a member of Foo type: member Bar::m_foo, so one can't
put Bar definition before Foo definition.

As a result, a compiler rejects
friend void Bar::bar(); // in class Foo

Is there any solution for this problem?

------ foo.cpp ------
class Uam
{
static void uam() {}
};

class Bar;

class Foo
{
friend void Uam::uam();
friend void Bar::bar();
};

class Bar
{
Foo m_foo;
static void bar() {}
};
int main()
{
return 0;
}

---------------------

------ Compilation ------
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for
80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

foo.cpp
foo.cpp(11) : error C2027: use of undefined type 'Bar'
foo.cpp(6) : see declaration of 'Bar'
-------------------------


See this FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-39.12

In essence, you'll need to reverse the order of your classes: forward
declare Foo, then define Bar making Bar::m_foo a pointer (preferably a
smart pointer such as std::tr1::scoped_ptr aka boost::scoped_ptr), and
then define Foo.

Cheers! --M

Jun 6 '06 #2

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

Similar topics

5
by: Marijn | last post by:
I'd like to know how compilers usually handle static variables that are declared inside a function (as opposed to static class-members). Like in: int counter(){ static int c=0; ++c; return c;...
12
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always...
3
by: CoolPint | last post by:
After upgrading to gcc 3.4.2 from gcc 3.2.3, I got compiler errors that I could not figure out. After reading other postings, I learned that my coding was not compliant to the standard in the first...
3
by: Bonj | last post by:
Very sorry but this is *actually* another question about COMPILERS, I just put that title as bait, but HANG ON! this is the last one honestly Very simply Is there ANY way you can *download* an...
9
by: John Smith | last post by:
Hi, I have this problem. I have a page class in code behind called "WebForm1" and I have another class called "Class1". Now in Class1 I am creating ImageButton and I am adding event handler...
2
by: ngpbalaji | last post by:
Shall we call a "Static function" from one to other file in C programming? If yes ., Then Can anyone explain me ...How? I Think this is possible by pointers.. ? Is it possible without touching...
1
by: =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki | last post by:
We have here class of C++ language: namespace Apt { class Name1 { public: class Name2; }; } I 'm give ebove class Name2 so a declalation of class without a definition
3
by: tomPee | last post by:
Hi, I have the following problem: I am trying to make some sort of base class menu that i can then use to derive other menu's from. Those menu's should then be able to interact with each other....
4
by: John Doe | last post by:
Hi, I have a singleton class defined like this : class UIManager : public CSingleton<UIManager>, public CObject { protected: DECLARE_DYNAMIC(UIManager) friend class CSingleton<UIManager>;
1
by: reymon | last post by:
<?PHP //THIS IS MY DB CLASS CONNECTION class db { private $hostname; private $username; private $password; private $database; private $connect; private...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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
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...
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,...
0
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...

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.