friend of a class

Posted on 1999-01-18
Last Modified: 2010-04-16
I have defined two classes A and B (in different header and cpp files). I want to use the constructor of class B in the constructor of class A. I did this by declaring class A as a friend of class B. This works but i want to ask wether there is another possibility to use the constructor of class B in class A without using the friend statement
Thanks Thorsten
Question by:thorsten
LVL 15

Expert Comment

by:Tommy Hui
ID: 1184469
What exactly do you mean by using the constructor of B in class A?

Are the classes related by hierarchy (meaning, is class A derived from B)?

Are you trying to create an instance of B in A?

Author Comment

ID: 1184470
Hi thui,
The classes are independent. The constructor of class A should throw an exception from the type of class B if the arguments it gets during initilization are not valid.
This is the constructor of class A
A::A(int a, int b, int c)
day =a;
if (korrekt()==0) throw Falsches_Datum(day,month,year);
korrekt() checkes wether the date is valid or not.
I did this declaring class A and B as friends. My question is which other possibilities do exist to use the constructor Falsches_Datum(..) in the constructor of class A.
Thank you

Expert Comment

ID: 1184471
As i know that is enough to declare class B ih header-file of class A. Then you can use B-constructor in A-methods. Constructor is public.

Expert Comment

ID: 1184472
really don't understand your question either,
if the constructor of class B is public, why does class A have to be a friend to call it.

the 'friend' keyword have only one meaning, which is not valid in this case.

it gives access for a class'es private!! members/methods,
for a function, another class/struct/union etc.
and nothing else.


Expert Comment

ID: 1184473
You should always try to avoid using Friend classes. It is the underminer of encapsulation. Like Koskia said, a friend class has access to a class's private members/methods.
To solve your problem, just make the constructor of the B-class public and include the header of the B-class in the A-class. Then the A-class has enough access to the B-class.

PS: if there's no way to avoid friend classes, check your software design, maybe it needs an update...

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

LVL 12

Expert Comment

ID: 1184474
To answer your specific question, you do not need to use the friend statement as the constructor should be public in both A and B (if not ask yourself how anyone is ever to create an instance of class A or B). Class B simply needs to call the constructor of class A.



Expert Comment

ID: 1184475
Make sure the constructor of class B is really public.
There must be word 'public:' above the definition of the constructor of B in .h file.

Best regards

Author Comment

ID: 1184476
Sorry I made a mistake. I have declared my constructor without the statement public. After Declaring it public everything works well.
Thank you

Expert Comment

ID: 1184477
Does this meen I get the points?

Accepted Solution

asar earned 200 total points
ID: 1184478
Constructor as a class can't be called using a friend specifier. So that you can't be able to use it. The main definition of a constructor is a member function ( not a friend function ). A constructor is called only when an object of that class is instanciated. No way you can use as a friend fuction.

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now