G
gordon.is.a.moron
Hello,
I have a few questions regarding classes in C++. First of all I have
two classes. I have a default constructor that assigns default values:
//class D.h
class D
{
public:
D(int id = 0, int sn =0);
}
I want to associate Class D with Class E:
//class E.h
class E
{
private:
D instance_;
}
//class E.cpp
{
void E::SendData()
{
D instance_(50.12);
}
}
However, the D instance_ delcaration calls the default constructor
with default values. So as well as calling the constructor in a
function in class E, the default constructor is called first with
default values. What is the correct way of associating an instance
with the other object without invoking the default constructor? The
isue is I can't really set those parameters in the declaration, and
I'm not sure I really should anyway, it doesn't seem correct.
And in a related question, I'm not entirely clear when you would call
D instance_(50,12) and D *instance_ = new D(50,12). I was doing this
initially as I thought using pointers would be more efficient over
references, but I'm not sure this is true. As far as I can gather I
should use references as much as possible and only use pointers when I
need to change what I'm pointing to, since I can't "move" a reference.
And also I would guess when I want to create an indeterminate number
of objects at runtime dynamically, which would presumably force me to
use pointers as this is what new returns in C++ when creating objects
of classes.
Regards,
Gordy.
I have a few questions regarding classes in C++. First of all I have
two classes. I have a default constructor that assigns default values:
//class D.h
class D
{
public:
D(int id = 0, int sn =0);
}
I want to associate Class D with Class E:
//class E.h
class E
{
private:
D instance_;
}
//class E.cpp
{
void E::SendData()
{
D instance_(50.12);
}
}
However, the D instance_ delcaration calls the default constructor
with default values. So as well as calling the constructor in a
function in class E, the default constructor is called first with
default values. What is the correct way of associating an instance
with the other object without invoking the default constructor? The
isue is I can't really set those parameters in the declaration, and
I'm not sure I really should anyway, it doesn't seem correct.
And in a related question, I'm not entirely clear when you would call
D instance_(50,12) and D *instance_ = new D(50,12). I was doing this
initially as I thought using pointers would be more efficient over
references, but I'm not sure this is true. As far as I can gather I
should use references as much as possible and only use pointers when I
need to change what I'm pointing to, since I can't "move" a reference.
And also I would guess when I want to create an indeterminate number
of objects at runtime dynamically, which would presumably force me to
use pointers as this is what new returns in C++ when creating objects
of classes.
Regards,
Gordy.