Solved

friend of a class

Posted on 1999-01-18
10
275 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
Independent Software Vendors: 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

Technology Partners: 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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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.

737 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