Solved

Derived class with default function parameters: needs explanation please !

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exception thrown at 0x00007FFD5BC81F28 7 53
Problem with SqlConnection 4 184
HTTPSendRequest with WinINet delays on first call 11 78
Adding items to a C# list incrementally 5 58
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. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

679 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