Re: a question about classes
From: Mike Tyndall (swtyndall_at_hotmail.com)
Date: 08/09/04
- Next message: Old Wolf: "Re: a question about classes"
- Previous message: rossum: "Re: compilation timing questions"
- In reply to: johnny_at_n0sq.us: "a question about classes"
- Next in thread: Chris \( Val \): "Re: a question about classes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 8 Aug 2004 17:44:48 -0500
<johnny@n0sq.us> wrote in message
news:0j8Rc.12119$cK.7661@newsread2.news.pas.earthlink.net...
> I'm not sure where to begin here but I'll start with showing you the code
I
> have questions about:
>
> class X
> {
> public:
> X(); //default constructor
> X(double iD, char iS); //explicit constructor
These constructors have no bodies. I'm going to assume that the first is
{ } and the second is { mD = iD; mS = iS; }
>
> X iF() const; //conversion method
>
> private:
> double mD;
> char mS;
> };
>
> X X::iF() const
> {
> switch(mS)
> {
> case 'F': return X(mD, 'F');
> case 'C': return X(mD * 2, 'F');
> case 'K': return X(mD * 3, 'F');
> }
> }
>
> My question is about the definition of <X iF() const>. If I understand
> correctly, when iF() is called, it, in turn, calls the explicit
constructor
> X(double, char) that passes different parameter values based on the value
> of mS. If so, doesn't calling the constructor result in mD and mS being
> modified? If so, then why is the keyword return being used? If I
understand
> correctly, constructors don't return values so how can the keyword return
> be appropriate here?
1) You defined the constructors X() and X(double iD, char iS).
2) In the switch block, you called the second constructor.
3) When a constructor is called, it creates a new object.
4) The function iF() const has a return value of X, meaning it returns an X
object to the caller. The body of the function returns a different new X
object based on the value of the calling object's mS member variable.
5) The constructor called in the switch block's first case creates a new X
object using the values in the constructor's arguments (this->mD, 'F') to
assign values to a new X object's member variables. The member variables
have the following values:
mD == the mD of the calling X object.
mS == 'F'
This object is returned to the caller.
Here's an example, using your code (plus some alterations and comments to
help you see what's going on) and main().
//BEGIN CODE
//<iostream> needed for cout
#include <iostream>
using std::cout;
class X
{
public:
//these constructors had no body,
//so I added them.
X() { }
//these are constructors. If you
//call the one below, for example
//like X(5.75424, 'B'), it creates
//a new X object and assigns
//values to that object's member
//variables.
X(double iD, char iS) {
mD = iD;
mS = iS;
}
X iF() const;
//function to print variable values
void printVals() const;
private:
double mD;
char mS;
};
X X::iF() const
{
printVals();
switch(mS)
{
//X(mD, 'F') calls a constructor.
//in the class declaration, you
//declared and defined this
//constructor. The other X()
//are also constructor calls.
case 'F': return X(mD, 'F');
case 'C': return X(mD * 2, 'F');
case 'K': return X(mD * 3, 'F');
}
}
//body of printVals()
void X::printVals() const {
cout << "Member variable values: "
<< mD << " " << mS << "\n";
}
int main() {
X x1(5.5, 'C');
/*
When x1 calls iF(), the function uses x1's
member variables in the switch statement.
Because mS has the value 'C', the second
case is executed. This calls the constructor
X(mD * 2, 'F'), which creates a new X
object. return then returns the object to the
caller. The new object's member variables
have the values 11.0 (mD * 2) and 'F'.
These are assigned to an X object called x2.
*/
X x2 = x1.iF();
x2.printVals();
return 0;
}
//END CODE
Hope that explains it.
//mike tyndall
- Next message: Old Wolf: "Re: a question about classes"
- Previous message: rossum: "Re: compilation timing questions"
- In reply to: johnny_at_n0sq.us: "a question about classes"
- Next in thread: Chris \( Val \): "Re: a question about classes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|