Solved

friend of a class

Posted on 1999-01-18
10
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

756 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