In your original code a was declared const.Quote:
I am confused what do you mean "(you can't pass a by non-const reference)".
I have shown you the code below, we can pass non-const reference to method goo. Any comments?
Printable View
In your original code a was declared const.Quote:
I am confused what do you mean "(you can't pass a by non-const reference)".
I have shown you the code below, we can pass non-const reference to method goo. Any comments?
Thanks Lindley,
1.
Code:#include <string>
using namespace std;
void foo1 (const string& abc)
{
return;
}
string foo2()
{
return "temp string";
}
void goo()
{
// passing literal to const string reference
foo1 ("MyAbc");
// passing temporary to const string reference
foo1 (foo2());
return;
}
laserlight is correct, and I have written code to prove. Compile ok in Visual Studio 2008.Quote:
Originally Posted by Lindley
2.
Can you show us what is the purpose to define a pure virtual destructor please?Quote:
Originally Posted by Lindley
regards,
George
Hi laserlight,
Using pure virtual destructor in base class does not have any issues. Here is my code in Visual Studio 2008, compile ok. :-)
Anyway, could you share your experience about what is the function of a pure-virtual destructor and in what situations do we need please?
Code:#include <string>
#include <iostream>
using namespace std;
class GraphPart
{
public:
virtual void setField(std::string fieldname, std::string value)=0;
virtual bool cmpField(std::string fieldname, std::string value) const=0;
virtual const char* getField(std::string fieldname) const=0;
virtual ~GraphPart() = 0;
};
class GraphNode: public GraphPart
{
public:
// data is a mix of non-pointer primitives and STL containers, nothing else
void setField(std::string fieldname, std::string value);
bool cmpField(std::string fieldname, std::string value) const;
const char* getField(std::string fieldname) const;
};
class GraphEdge: public GraphPart
{
public:
// data is non-pointer primitives
void setField(std::string fieldname, std::string value);
bool cmpField(std::string fieldname, std::string value) const;
const char* getField(std::string fieldname) const;
};
Quote:
Originally Posted by laserlight
regards,
George
I believe this has come up several times before on this forum. A pure virtual destructor may be used when you want the class to be an abstract base class, but none of the member functions are suitable candidates to be declared pure virtual. However, the base class still needs an implementation for the destructor since it will be invoked from the derived class' destructor. Lindley's problem was this lack of an implementation for the pure virtual destructor.Quote:
Can you show us what is the purpose to define a pure virtual destructor please?
Thanks angelorohit,
I do not think at least Visual Studio 2008 follows this rule. You can refer to my post #18 and see even if the derived class is defined, the destructor of base class could still be pure virtual.
Any comments?
Quote:
Originally Posted by angelorohit
regards,
George
You're reading the rule wrongly.Quote:
I do not think at least Visual Studio 2008 follows this rule. You can refer to my post #18 and see even if the derived class is defined, the destructor of base class could still be pure virtual.
Any comments?
Hi TheCPUWizard,
Your code shows why needs to define it virtual, but not why needs to define it pure virtual...
Any comments?
Quote:
Originally Posted by TheCPUWizard
regards,
George
Thanks laserlight,
You mean variable a is const or the input parameter type of goo in derived class is const?
Quote:
Originally Posted by laserlight
regards,
George
There is only one variable named a in your original code, and it is a const int. The variable named a in your new example is a non-const int, so it can be passed by non-const reference.Quote:
You mean variable a is const or the input parameter type of goo in derived class is const?
angelorohit wrote: "since you are passing a by value (you can't pass a by non-const reference)". In other words, you are passing a by value, and that is fine.
However, if you try to pass a by non-const reference, then it will not work. To pass a by non-const reference, you have to change the input parameter of goo() to a non-const reference. Then you pass in a, which is a const int.
Hi laserlight,
I read your comments a couple of time, but confused.
1. We need pure virtual function when we are implementing a pure virtual class but none of other functions are appropriate to define as pure virtual;
Seems it is ok and reasonable to define a destructor pure virtual.
2. "However, the base class still needs an implementation for the destructor since it will be invoked from the derived class' destructor."
Seems not ok to define a destructor pure virtual.
What is the truth? Any comments?
Quote:
Originally Posted by laserlight
regards,
George
Basically:Quote:
2. "However, the base class still needs an implementation for the destructor since it will be invoked from the derived class' destructor."
Seems not ok to define a destructor pure virtual.
What is the truth? Any comments?
Code:class X
{
public:
virtual ~X() = 0; // pure virtual destructor
};
X::~X() {} // implementation of the pure virtual destructor
No laserlight,
For your below quoted comments, we can pass a non-const reference to goo without changing the original code, here is my code. Any comments?
i.e. we can pass non-const reference type to non-const value type.
Code:class Base {
public:
virtual int goo (const int input) {return 200;}
};
class Derived : public Base {
public:
virtual int goo (int input) {return 200;} // change const property of input parameter
};
int main()
{
Derived d;
int a = 100;
int& ra = a;
d.goo (ra); // pass non-const reference type to non-const value type
return 0;
}
Quote:
Originally Posted by laserlight
regards,
George
Thanks laserlight,
1. Pure virtual function can have an implementation? e.g. your designed destructor?
2. In your code, seem two purposes are achieved
A. base class has pure virtual function (in your sample code, the destructor), which can not be created directly;
B. base class's destructor has implementation body, which could be invoked from derived class's destructor.
Right?
Quote:
Originally Posted by laserlight
regards,
George
Your code does not really illustrate the point that angelorohit was trying to make. In fact, angelorohit's point was just a passing comment. Furthermore, your code is fundamentally different from your original.Quote:
For your below quoted comments, we can pass a non-const reference to goo without changing the original code, here is my code. Any comments?
If you want something closer to the original, try compiling:
Notice that a is now a const int.Code:class Base {
public:
virtual int goo (const int input) {return 200;}
};
class Derived : public Base {
public:
virtual int goo (int input) {return 200;} // change const property of input parameter
};
int main()
{
Derived d;
const int a = 100;
int& ra = a;
d.goo (ra); // pass non-const reference type to non-const value type
return 0;
}
Your code does not really illustrate the point that angelorohit was trying to make. In fact, angelorohit's point was just a passing comment. Furthermore, your code is fundamentally different from your original.Quote:
For your below quoted comments, we can pass a non-const reference to goo without changing the original code, here is my code. Any comments?
If you want something closer to the original, try compiling:
Notice that a is now a const int.Code:class Base {
public:
virtual int goo (const int input) {return 200;}
};
class Derived : public Base {
public:
virtual int goo (int input) {return 200;} // change const property of input parameter
};
int main()
{
Derived d;
const int a = 100;
int& ra = a;
d.goo (ra); // pass non-const reference type to non-const value type
return 0;
}
Only in this case where a pure virtual destructor is involved, otherwise an implementation for a pure virtual function does not make sense since it should then just be a virtual function.Quote:
1. Pure virtual function can have an implementation? e.g. your designed destructor?
Yes.Quote:
2. In your code, seem two purposes are achieved
A. base class has pure virtual function (in your sample code, the destructor), which can not be created directly;
B. base class's destructor has implementation body, which could be invoked from derived class's destructor.
Hi laserlight,
I read again. This is the rule,
--------------------
A destructor can be declared virtual (10.3) or pure virtual (10.4); if any objects of that class or any derived class are created in the program, the destructor shall be defined.
--------------------
My code is posted in #18. It breaks this rule.
Anything wrong? Appreciated if you could point out.
Quote:
Originally Posted by laserlight
regards,
George