Solved

friend of a class

Posted on 1999-01-18
10
270 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

932 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

10 Experts available now in Live!

Get 1:1 Help Now