 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
artie Guest
|
Posted: Wed Apr 20, 2005 9:35 am Post subject: Type Conversion to a Base Template Class |
|
|
I am unable to get this to compile. The error messages are:
in constructor `UseSomething::UseSomething()':
error: cannot allocate an object of type `ValueConstraint<float>'
error: because the following virtual functions are abstract:
error: T ValueConstraint<T>::getRandomConstraintValue() const [with T =
float]
-----------------------------
template <typename T>
class ValueConstraint
{
public:
virtual T getRandomConstraintValue() const = 0;
};
template <typename T>
class ValueDomain : public ValueConstraint<T>
{
public:
ValueDomain(T minVal, T maxVal) :
min_(minVal), max_(maxVal)
{ }
T getRandomConstraintValue() const
{
return static_cast<T>(1.0); // return some value between min_ and max_
}
private:
T min_;
T max_;
};
template <typename T>
class Something
{
public:
Something(ValueConstraint<T> vc)
{
someValue_ = vc.getRandomConstraintValue();
}
private:
T someValue_;
};
class UseSomething
{
public:
UseSomething() :
something_(ValueDomain<float>(0.0, 1.0))
{ }
private:
Something<float> something_;
};
-----------------------------
I am using similar logic found on page 770 in the book C++ PL by B.
Stroustrup. Does anyone know what might be the problem here.
Thanks in Advance,
--Artie Eoff
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Cyril Guest
|
Posted: Wed Apr 20, 2005 4:49 pm Post subject: Re: Type Conversion to a Base Template Class |
|
|
Look there :
class ValueDomain : public ValueConstraint<T>
{
....
===> Should it be virtual here so function signature match ?
T getRandomConstraintValue() const
{
return static_cast<T>(1.0); // return some value
between min_ and max_
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Murali Guest
|
Posted: Wed Apr 20, 2005 4:54 pm Post subject: Re: Type Conversion to a Base Template Class |
|
|
artie wrote:
| Quote: | I am unable to get this to compile. The error messages are:
in constructor `UseSomething::UseSomething()':
error: cannot allocate an object of type `ValueConstraint<float>'
error: because the following virtual functions are abstract:
error: T ValueConstraint<T>::getRandomConstraintValue() const [with T
=
float]
-----------------------------
template <typename T
class ValueConstraint
{
public:
virtual T getRandomConstraintValue() const = 0;
};
template
class ValueDomain : public ValueConstraint
{
public:
ValueDomain(T minVal, T maxVal) :
min_(minVal), max_(maxVal)
{ }
T getRandomConstraintValue() const
{
return static_cast
max_
}
private:
T min_;
T max_;
};
template <typename T
class Something
{
public:
Something(ValueConstraint
{
someValue_ = vc.getRandomConstraintValue();
}
private:
T someValue_;
};
class UseSomething
{
public:
UseSomething() :
something_(ValueDomain<float>(0.0, 1.0))
{ }
private:
Something<float> something_;
};
-----------------------------
I am using similar logic found on page 770 in the book C++ PL by B.
Stroustrup. Does anyone know what might be the problem here.
|
ValueConstraint<T> is an abstract class. You cannot define objects of
this class. You need to change the constructor of "Something" to take a
pointer to ValueConstraint<T> and also modify "UseSomething"
appropriately.
Regards,
Murali
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gerhard Menzl Guest
|
Posted: Thu Apr 21, 2005 1:56 am Post subject: Re: Type Conversion to a Base Template Class |
|
|
artie wrote:
| Quote: | I am unable to get this to compile. The error messages are:
in constructor `UseSomething::UseSomething()':
error: cannot allocate an object of type `ValueConstraint<float>'
error: because the following virtual functions are abstract:
error: T ValueConstraint<T>::getRandomConstraintValue() const [with T =
float]
-----------------------------
template <typename T
class ValueConstraint
{
public:
virtual T getRandomConstraintValue() const = 0;
};
template
class ValueDomain : public ValueConstraint
{
public:
ValueDomain(T minVal, T maxVal) :
min_(minVal), max_(maxVal)
{ }
T getRandomConstraintValue() const
{
return static_cast
}
private:
T min_;
T max_;
};
template <typename T
class Something
{
public:
Something(ValueConstraint
{
someValue_ = vc.getRandomConstraintValue();
}
private:
T someValue_;
};
class UseSomething
{
public:
UseSomething() :
something_(ValueDomain<float>(0.0, 1.0))
{ }
private:
Something<float> something_;
};
-----------------------------
I am using similar logic found on page 770 in the book C++ PL by B.
Stroustrup. Does anyone know what might be the problem here.
|
The name of the problem is "slicing". You are trying to pass an object
of type ValueDomain<float> by value to Something<float>::Something(),
which expects an object of type ValueConstraint<float>, which is an
abstract base class of ValueDomain<float>. Passing by value means
copying, but copying a derived class object to a base class object would
cause the derived part to be sliced off and a base class instance to be
created. Now abstract classes (classes with at least one pure virtual
function) cannot be instantiated, which is what the error message tells you.
If you look closely at Stroustrups example, you will find that the
function that corresponds to your Something<float> constructor takes its
argument by *reference*. No copying, and therefore no slicing takes
place. Polymorphism only works through pointers and references. Change
the constructor definition to
explicit Something(ValueConstraint<T> const& vc)
{
someValue_ = vc.getRandomConstraintValue();
}
and you will be fine. Note that I have added the explicit keyword, which
will prevent implicit conversions from ValueConstraint<T> const& to
something, and a const because Something<T>::Something only calls a
const member function on its argument and does not modify it.
--
Gerhard Menzl
#dogma int main ()
Humans may reply by replacing the thermal post part of my e-mail address
with "kapsch" and the top level domain part with "net".
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Thu Apr 21, 2005 1:57 am Post subject: Re: Type Conversion to a Base Template Class |
|
|
artie wrote:
| Quote: | I am unable to get this to compile. The error messages are:
in constructor `UseSomething::UseSomething()':
error: cannot allocate an object of type `ValueConstraint<float>'
error: because the following virtual functions are abstract:
error: T ValueConstraint<T>::getRandomConstraintValue() const [with T =
float]
snip
template <typename T
class Something
{
public:
Something(ValueConstraint
|
As ValueConstraint<T> is an abstract class, you can't pass it by-value.
Passing it by-(const-)reference looks ok for your purposes and avoids
the problem entirely:
Something(const ValueConstraint<T>& vc)
HTH,
Alberto
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gerhard Menzl Guest
|
Posted: Thu Apr 21, 2005 12:09 pm Post subject: Re: Type Conversion to a Base Template Class |
|
|
Cyril wrote:
| Quote: | Look there :
class ValueDomain : public ValueConstraint<T
{
...
===> Should it be virtual here so function signature match ?
T getRandomConstraintValue() const
{
return static_cast<T>(1.0); // return some value
between min_ and max_
}
|
No, that has nothing to do with the problem. The rule is: once virtual,
always virtual (i.e. in all derived classes), no matter whether you
specify it explicitly or not. It is recommended to spell it out for
clarity even when it's not necessary, though.
--
Gerhard Menzl
#dogma int main ()
Humans may reply by replacing the thermal post part of my e-mail address
with "kapsch" and the top level domain part with "net".
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
artie Guest
|
Posted: Thu Apr 21, 2005 4:50 pm Post subject: Re: Type Conversion to a Base Template Class |
|
|
Thanks everyone for your replies. I overlooked the ByReference in C++ PL;
and it definately makes sense now. I am confident that it will work.
As far as the virtual keyword being used in the derived class, wouldn't
this add some overhead to the derived class in the form of a new virtual
table being created for that class? Also, it would allow another class to
derive from it and possibly overload that function once more(which isn't
always desirable).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gerhard Menzl Guest
|
Posted: Sat Apr 23, 2005 2:02 am Post subject: Re: Type Conversion to a Base Template Class |
|
|
artie wrote:
| Quote: | As far as the virtual keyword being used in the derived class,
wouldn't this add some overhead to the derived class in the form of a
new virtual table being created for that class? Also, it would allow
another class to derive from it and possibly overload that function
once more(which isn't always desirable).
|
It makes no difference whatsoever, except for the human reader. A
function that overrides (not overloads!) a virtual function in a base
class is itself virtual, no matter whether you specify it or not. There
is no such thing as final or sealed in C++.
--
Gerhard Menzl
#dogma int main ()
Humans may reply by replacing the thermal post part of my e-mail address
with "kapsch" and the top level domain part with "net".
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|