Solved

Derived class with default function parameters: needs explanation please !

Posted on 2004-09-01
5
206 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now