Let's say you have made a class called threevector which takes stores three doubles.

Then you define another class, a fourvector, which stores four doubles.

I have seen this syntax used for the constructor of the fourvector:

fourvector(double fourth, threevector one_two_three): threevector (one_two_three)
{
fourth = 0.0;
}

I understand that the fourvector object takes in a double and a threevector as its arguments. But I haven't defined any constructor called threevector (one_two_three) in the threevector class. So what does the compiler do?

do you have a class threevector?

most probably fourvector contains the threevector member variable

fourvector constructor is initializing the three vector constructor

The code you posted would indicate that fourvector is a class derived from the class threevector. The constructor of fourvector simply forwards the one_two_three parameter to its base class constructor.

Remark: "I have seen this syntax used for the constructor of the fourvector:"
Run! Just run away from this code! There are so many things that are wrong with it that its not worth wasting time correcting it. Just run, before it poisons your mind.

simple sample of fourvector containing a threevector

class threevector
{
public:
    threevector(double a_, double b_, double c_)
    :_a(a_)
    ,_b(b_)
    ,_c(c_)
    {
    }
    threevector(const threevector &val_)
    :_a(val_._a)
    ,_b(val_._b)
    ,_c(val_._c)
    {
    }
private:
    double _a;
    double _b;
    double _c;
};

class fourvector
{
public:
    fourvector(double forth_, threevector three_vector_)
    :three(three_vector_)
    ,forth(forth_)
    {
    }
private:
    threevector three;
    double forth;
};

int main()
{
    threevector three(1.0,2.0,3.0);
    fourvector(4.0,three);
    return 1;
}

The code you posted would indicate that fourvector is a class derived from the class threevector. The constructor of fourvector simply forwards the one_two_three parameter to its base class constructor.

Remark: "I have seen this syntax used for the constructor of the fourvector:"
Run! Just run away from this code! There are so many things that are wrong with it that its not worth wasting time correcting it. Just run, before it poisons your mind.

Funny that you mention that. I think I have seen this type of example somewhere from a -textbook-. Kind of "funny" (being facetious) that a text book would give a bad example as the one you are saying to run away from.

I suppose this wouldn't be the first time I've head of this occurring. Difference between what you "can do" and what you "should really be doing".

Out of curiosity, why would this set up be so bad?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.