Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Derived class with default function parameters: needs explanation please !

Posted on 2004-09-01
5
Medium Priority
?
253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

610 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