Solved

Derived class with default function parameters: needs explanation please !

Posted on 2004-09-01
5
223 Views
Last Modified: 2010-04-01
Ah; hello !

This is a hardcore C++ question that I am hoping can be resolved by someone who can give me a solid explanation.

Please peruse this code:

class A
{
public:
    A() {}
    virtual int GetVal(int a = 1) { return a; }
};

class B : public A
{
public:
    B() {}
    virtual int GetVal(int b = 2) { return b; }
}

main()
{
    A *x = new B();
    int r = x->GetVal();
}

Confusion:  r is 1.  Now I know this, but would like solid explanation and clarification as to *why*.

Firstly we are dealing with pointers, so that allows run time binding, right ?  And we are creating a B object but assigning it to an A pointer.  So why is the output not 2 ?  Arghhhh !

TIA
0
Comment
Question by:mrwad99
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 11957146
The default parameter's value is deducted from the pointer type (where else should the compiler get that info from?) - consider the following:

class I
{
public:
   virtual int GetVal(int a = 0) = 0;
};


class A : public I
{
public:
   A() {}
   virtual int GetVal(int a = 1) { return a; }
};

class B : public A
{
public:
   B() {}
   virtual int GetVal(int b = 2) { return b; }
}

main()
{
   I *x = new B();
   int r = x->GetVal();
}

'r' will be (in fact is) 0...
0
 
LVL 19

Author Comment

by:mrwad99
ID: 11957212
So you are saying that as a general rule, default parameters are resolved at compile time always ?

I have just tried the same without pointers and the default value is taken from the type being assigned *to*, in this case I.

I am still a little confused however as to why it is taking the default from I when it can clearly see that I want to make an B object...

0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 11957331
>> So you are saying that as a general rule, default parameters are resolved at compile time always ?

Yes. Functions have to be called with all their parameters, so offering 'default's is just a convenience feature of the language.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 11957347
OK.  I guess I need to re-read about virtual functions and late binding then.

Thanks.
0
 
LVL 86

Expert Comment

by:jkr
ID: 11957384
No, in general, you would be right. I't's just that 'default parameter' thingy that messes things up here.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

785 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