Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Classes

Posted on 1999-07-21
18
Medium Priority
?
225 Views
Last Modified: 2010-04-02
Let say I have class A based on class B

And I have to pass Pointer of class B to somebody.

How to do it???

Example:

Class A: public B
{
 ….
}
==================================

void function DoSomething(B&);

===========================================================A MyVar;

 DoSomething(????);
0
Comment
Question by:msoft
[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
  • 6
  • 4
  • 4
  • +2
18 Comments
 
LVL 6

Expert Comment

by:Triskelion
ID: 1200760
Prototype
   void DoSomething(B * pB);


Usage

   {
   static B xBeeClass;
   DoSomething(&B);
   }
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 1200761
You can also use a reference

Prototype
   void DoSomething(B & refB);


Usage

   {
   static B xBeeClass;
   DoSomething(B);
   }


0
 

Author Comment

by:msoft
ID: 1200762
Triskelion,

I don`t want to work with Class B, I want to use only class A that Based on B.  

May be I don`t understand your Example.

Michael.
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!

 

Author Comment

by:msoft
ID: 1200763
Triskelion,

I don`t want to work with Class B, I want to use only class A that Based on B.  

May be I don`t understand your Example.

Michael.
0
 

Author Comment

by:msoft
ID: 1200764
Triskelion,

I don`t want to work with Class B, I want to use only class A that Based on B.  

May be I don`t understand your Example.

Michael.
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 1200765
What is the pointer you need to pass?
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1200766
struct B {};
struct A : public B {};

void f1(B*);
void f2(B&);

void f3(A*);
void f4(A&);

void g()
{  
  B b;
  A a;

  f1(&a); // <- what you're looking for i think
  f1(&b);

  f2(a);
  f2(b);

  f3(&a);
  f3(&b); // illegal

  f4(a);
  f4(b); // illegal
}
0
 
LVL 22

Expert Comment

by:nietod
ID: 1200767
msoft, publicly inherited classes will be automatically upcasted.  Thus in

Class A: public B
{
   ….
}

A a;
A *aPtr = &a;

"a" can be used as either a class A object or as a class B object.  Similarly, aPtr can be used as a pointer to a class A object or as a class B object.   That is what kangaroo's code above is showing.
0
 

Author Comment

by:msoft
ID: 1200768
KangaRoo &  Nietod !

Thanks, actually you answered my question.

Nietod, but what happens if My class is inherited from two Classes

Like

class A: public B,C
{
 .......
}

So &A is it like &B or &C ???

 (Or it converted automatically like
 B& Baddr, C& Caddr
Baddr = A; (Actually B)
Caddr = A; (Actually C) )

Post your answer please. (So that I could accept it).

thanks.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1200769
>> So &A is it like &B or &C ???
Yes.

Okay that wasn't very helpful, but the answer is both &B and &C.  Lets switch to pointers (*) instead of references (&) because it is easier to explain, and the rules and information are really the same.  For a given object of type A, you can generate a pointer of type A*, B* or C*.  However these pointers might not have the same address, that is, they might not have the same numerical value.  The C++ standard requires that the compiler be able to convert an A* into either a B* or a C* for the same object, however it does not say how the compiler must do so.  In most cases, the A object, that is, a multiply inheirited object, will be stored in memory as one of its base objects, followed by the other.  Thus you A object will usually be stored in memory as a B object followed by a C object.  Thus the compiler can generate a pointer to the B portion of A by simply using the pointer to the A object.  It can generate a pointer to the C portion of A by taking the pointer to A and adding on the size of the B object to get a pointer to the C portion of A.  Now the same thing is true of references because references are really pointers, only the compiler handles the referecing and dereferencing for you.  Make sense?
0
 

Expert Comment

by:poloi
ID: 1200770
Suppose class B is the base class of class A:

class A: public class B {
......
};

Suppose the prototype and implementation of DoSomething() is really based on Class B, like this:

void DoSomething(B& objectB) {
.....
}

Then we need not do anything with the objectA belows:

void main() {

A objectA;

DoSomething(objectA);

}

objectA will be automatically casted to class B because B is the base class of A and  so objectA has already contained the informations necessary to those function which needs to access class B.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1200771
poloi, thanks for clearing that up for us.  We obviously had no idea.
0
 

Expert Comment

by:poloi
ID: 1200772
Hahaha... of course you have no idea, because just from your question I know you don't know object-oriented paradigm at all.

0
 

Author Comment

by:msoft
ID: 1200773
Nietod,
go ahead and Post your answer. (so I could accept it).

Poloi, what wrong with Nietod comment?

Michael.


0
 

Expert Comment

by:poloi
ID: 1200774
msoft,

There is no problem at all to Nietod's code and answer. He is absolutely right. But I was really angry about  "We obviously has no idea"... NO IDEA?! No idea for what? OBVIOUSLY anybody who knows C++ knows what the code doing. You rejected my answer, but I tell you what my answer doing has exactly the same concept as Nietod's code doing. The reason I put my answer here is you don't seem to know object-oriented concept too well because you were asking those questions, I shown you an alternative for Nietod's code.

0
 

Author Comment

by:msoft
ID: 1200775
poloi,
Nietod was the first,
so I think that he should get the points...

Michael.
0
 

Expert Comment

by:poloi
ID: 1200776
I agree. Of course he should get it.
The score is not my concern at all.

0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 1200777
Sorry, I didn't respond earlier.  I've been moving and the movers were over a week late delivering our stuff and then caused lots of damage so it has been a while...

poloi,

>> But I was really angry about  "We
>>      obviously has no idea"

The reason I said that is that your answer was exactly what kangroo and myself had already stated.  In this forum, it is considered "unethical" to answer a question by presenting information that another expert previously provided.  In addition, msoft, requested that I answer, so it is also considered "unethical" for you to answer at that point.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

705 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