Solved

friend of a class

Posted on 1999-01-18
10
273 Views
Last Modified: 2010-04-16
Hello,
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
0
Comment
Question by:thorsten
10 Comments
 
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?
0
 

Author Comment

by:thorsten
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;
month=b;
year=c;
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
Thorsten
0
 
LVL 1

Expert Comment

by:MCoroli
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.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Expert Comment

by:koskia
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.


0
 
LVL 2

Expert Comment

by:MelissaC
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...

0
 
LVL 12

Expert Comment

by:rwilson032697
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.

Cheers,

Raymond.
0
 
LVL 1

Expert Comment

by:Seeker092397
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
0
 

Author Comment

by:thorsten
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
Thorsten
0
 
LVL 1

Expert Comment

by:Seeker092397
ID: 1184477
Does this meen I get the points?
0
 
LVL 1

Accepted Solution

by:
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

809 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