开发者

Can a class be used as a pointer?

in one of my assignments i was asked to create a lamp in c++ that contains bulbs and able to switch the bulbs at will. CLamp has an instant of CBulb inside of it. This is part of the solution for the lab:

in the CLamp class:

开发者_如何学CCLamp::CLamp(const CLamp& oldLamp)
{
    bptr  = new CBulb;
    *bptr = *(oldLamp.bptr);
}

and:

CBulb *CLamp::ExchangeBulb(CBulb *theNewBulb)
{
    CBulb *tmp = bptr;
    bptr = theNewBulb;

    return tmp;
}

in the main:

CLamp lamp1(*some number*);
CBulb *testbptr = new CBulb(*some other number*);
CBulb *temp = lamp1.ExchangeBulb(testbptr);
delete temp;

so what does CBulb *CLamp::ExchangeBulb(CBulb *theNewBulb) mean? What is ExchangeBulb a member function of? also does this mean that *Clamp is an object of type CBulb? thanks in advance for you time.


You have a class CLamp, and a class CBulb.

so what does "CBulb *CLamp::ExchangeBulb(CBulb *theNewBulb)" mean?

It defines a member function of CLamp called ExchangeBulb that takes a pointer to CBulb and returns a pointer to CBulb

What is ExchangeBulb a member function of?

ExchangeBulb is a member function of CLamp. It takes a pointer to a CBulb as an argunent.

also does this mean that *Clamp is an object of type CBulb?

No, it doesn't.

I suggest that you should read a good C++ book


CBulb *CLamp::ExchangeBulb(CBulb *theNewBulb)

ExchangeBulb is a member function of CLamp.It returns a pointer to CBulb class and takes a pointer to a CBulb as an argunent.


ExchangeBulb is a method in your CLamp class whose purpose is to take a new bulb and replace the old bulb. What you are doing when you call CBulb *temp = lamp1.ExchangeBulb(testbptr); is you are trying to replace the bulb(some number) with newly created bulb (Some other number).

ExchangeBulb is the member of CLamp. For obvious reasons since only Lamp can replace its bulb. CBulb* means you are returning a CBulb pointer.


ExchangeBulb is a function of CLamp. It changes the bulb and returns the old one.

also does this mean that *Clamp is an object of type CBulb?

No its not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜