473,466 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

virtual function

Hi,

Please consider the following classes:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Shape
{
......
......
public:
virtual void draw(CDC & aDC, COLORREF color, COLORREF fcolor, int
width, BOOL Filled = false) = 0;
};

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Line : public Shape
{
......
......
public:
void draw(CDC &dc, COLORREF color, COLORREF fcolor, int width,
BOOL Filled = false);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class ellipse : public Shape
{
......
......
public:
void draw(CDC & dc, COLORREF color, COLORREF fcolor, int width,
BOOL Filled = false);
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

My question is:

If I declared the member function "draw( )" inside the classes and
implemented the definitions of "draw( )" outside the classes, then
there will be an error messsage when compiling :

"redefinition of default parameter : parameter 5"

However, if I implemented all the definitions of "draw( )" inside the
classes, then the code can pass the compilation.

Would anybody please let me know what the issue is? Thakns a lot.

Jan 6 '06 #1
3 3029

hu*******@hotmail.com wrote:
Hi,

Please consider the following classes:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Shape
{
.....
.....
public:
virtual void draw(CDC & aDC, COLORREF color, COLORREF fcolor, int
width, BOOL Filled = false) = 0;
};

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Line : public Shape
{
.....
.....
public:
void draw(CDC &dc, COLORREF color, COLORREF fcolor, int width,
BOOL Filled = false);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class ellipse : public Shape
{
.....
.....
public:
void draw(CDC & dc, COLORREF color, COLORREF fcolor, int width,
BOOL Filled = false);
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

My question is:

If I declared the member function "draw( )" inside the classes and
implemented the definitions of "draw( )" outside the classes, then
there will be an error messsage when compiling :

"redefinition of default parameter : parameter 5"

However, if I implemented all the definitions of "draw( )" inside the
classes, then the code can pass the compilation.


A much simpler example of the same problem is the following

// Function declaration
void foo(int i = 42);

int main()
{
return 0;
}

// Function definition
void foo(int i = 42) {}

You are not allowed to define the default parameter in two places. The
solution is either

// Function declaration and definition
void foo(int i = 42) {}

int main()
{
return 0;
}

which is the equivalent of you defining your draw functions inside your
classes, or

// Function declaration
void foo(int i = 42) {}

int main()
{
return 0;
}

// Function definition
// Note default parameter not redefined
void foo(int i) {}

Gavin Deane

Jan 6 '06 #2

hu*******@hotmail.com wrote:
Hi,

Please consider the following classes:
class Shape {> public: virtual void draw(CDC & aDC, COLORREF color, COLORREF fcolor, int
width, BOOL Filled = false) = 0;
};
class Line : public Shape
{
public:
void draw(CDC &dc, COLORREF color, COLORREF fcolor, int width,
BOOL Filled = false);
} My question is:

If I declared the member function "draw( )" inside the classes and
implemented the definitions of "draw( )" outside the classes, then
there will be an error messsage when compiling :

"redefinition of default parameter : parameter 5"


Actually, your question has nothing to with "virtual functions". C++
doesnot allow redefinition of default arguments of functions. So once
they are defined in the function prototype, you should not specify them
again in the function definition.
<quote>
8.3.6p4 : A default argument shall not be redefined by a later
declaration (not even to the same value).
</quote>

Jan 7 '06 #3
Hi Gavin,

Thanks a lot for your suggestion and it did help. Thanks again.
Gavin Deane wrote:
Hi,

Please consider the following classes:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Shape
{
.....
.....
public:
virtual void draw(CDC & aDC, COLORREF color, COLORREF fcolor, int
width, BOOL Filled = false) = 0;
};

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Line : public Shape
{
.....
.....
public:
void draw(CDC &dc, COLORREF color, COLORREF fcolor, int width,
BOOL Filled = false);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class ellipse : public Shape
{
.....
.....
public:
void draw(CDC & dc, COLORREF color, COLORREF fcolor, int width,
BOOL Filled = false);
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

My question is:

If I declared the member function "draw( )" inside the classes and
implemented the definitions of "draw( )" outside the classes, then
there will be an error messsage when compiling :

"redefinition of default parameter : parameter 5"

However, if I implemented all the definitions of "draw( )" inside the
classes, then the code can pass the compilation.


A much simpler example of the same problem is the following

// Function declaration
void foo(int i = 42);

int main()
{
return 0;
}

// Function definition
void foo(int i = 42) {}

You are not allowed to define the default parameter in two places. The
solution is either

// Function declaration and definition
void foo(int i = 42) {}

int main()
{
return 0;
}

which is the equivalent of you defining your draw functions inside your
classes, or

// Function declaration
void foo(int i = 42) {}

int main()
{
return 0;
}

// Function definition
// Note default parameter not redefined
void foo(int i) {}

Gavin Deane


Jan 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
8
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
7
by: desktop | last post by:
This page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html start with the line: "Virtual functions allow polymorphism on a single argument". What does that exactly mean? I guess it...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.