Hello,
Why does my cast from Vector<class Float> to Vector<float> not work? It
won't compile,
template<class Float> class Vector
{
public:
Vector(Float x1,Float y1,Float z1):x(x1),y(y1) ,z(z1){}
inline Vector<float> operator() () const;
Float x,y,z;
};
template <class Float> inline Vector<float>
Vector<Float>:: operator() () const
{
return Vector<float>(( float)x,(float) y,(float)z);
}
int main()
{
Vector<double> pd(5.6,3.4,2.4) ;
Vector<float> pf=(Vector<floa t>)pd; /* compiler error here */
}
I'd ideally like to be able to cast a Vector<double> to a Vector<float>.
Jul 22 '05
21 2193
> You mean with VC7.0? I'm not surprised it doesn't work, but I don't understand what you mean by 'ignores the cast completely'.
As in 'reacts as though I had never written the cast operator to the Vector
class'.
"Makhno" <ro**@127.0.0.1 > wrote in message
news:bv******** **@news7.svr.po l.co.uk... You mean with VC7.0? I'm not surprised it doesn't work, but I
don't understand what you mean by 'ignores the cast completely'. As in 'reacts as though I had never written the cast operator to the
Vector class'.
Yes, that's nasty. Did you try using converting constructors instead?
Jonathan
"Jonathan Turkanis" <te******@kanga roologic.com> wrote in message: Yes, that's nasty. Did you try using converting constructors
instead?
You probably need a templated assignment operator too, if you do this.
Jonathan
> Did you try using converting constructors instead?
Yes, worked straight away. You're right - perhaps I don't need the cast
operator at all. You probably need a templated assignment operator too, if you do this.
No, as
Vector<float> pf=pd;
and
Vector<float> pf(pd);
are equivalent;
"Makhno" <ro**@127.0.0.1 > wrote in message
news:bv******** *@newsg4.svr.po l.co.uk... Did you try using converting constructors instead? Yes, worked straight away. You're right - perhaps I don't need the
cast operator at all.
You probably need a templated assignment operator too, if you do
this. No, as Vector<float> pf=pd; and Vector<float> pf(pd); are equivalent;
But that's just one use of the implicit conversion.
How about :
Vector<double> pd;
Vector<float> pf;
// More stuff here.
pf = pd;
?
I didn't suggest a templated assignement operator first because your
example didn't require it. But if you want a general-purpose
substitute for the conversion operator, you need it.
Jonathan
"Jonathan Turkanis" <te******@kanga roologic.com> wrote in message
news:bv******** ****@ID-216073.news.uni-berlin.de... "Makhno" <ro**@127.0.0.1 > wrote in message news:bv******** *@newsg4.svr.po l.co.uk... Did you try using converting constructors instead? Yes, worked straight away. You're right - perhaps I don't need the cast operator at all.
You probably need a templated assignment operator too, if you do this. No, as Vector<float> pf=pd; and Vector<float> pf(pd); are equivalent;
I didn't suggest a templated assignement operator first because your example didn't require it. But if you want a general-purpose substitute for the conversion operator, you need it.
I spoke too soon. It's non needed here either. But it's not because
of copy initialization.
Jonathan
> But that's just one use of the implicit conversion. How about:
Vector<double> pd; Vector<float> pf; // More stuff here. pf = pd;
?
Hmm, this still seems to work, how peculiar. Vector<> doesn't have an
assignment operator.
> I spoke too soon. It's non needed here either. But it's not because of copy initialization.
why then?
"Jonathan Turkanis" <te******@kanga roologic.com> wrote... "Makhno" <ro**@127.0.0.1 > wrote in message news:bv******** *@newsg4.svr.po l.co.uk... Did you try using converting constructors instead?
Yes, worked straight away. You're right - perhaps I don't need the
cast operator at all.
You probably need a templated assignment operator too, if you do this. No, as Vector<float> pf=pd; and Vector<float> pf(pd); are equivalent;
But that's just one use of the implicit conversion.
How about:
Vector<double> pd; Vector<float> pf; // More stuff here. pf = pd;
?
I didn't suggest a templated assignement operator first because your example didn't require it. But if you want a general-purpose substitute for the conversion operator, you need it.
Why? 'pf = pd' expression will result into the use of compiler-
generated assignment operator and a construction of a temporary
on the right side, no? It should be equivalent to
{ Vector<float> temp(pd); pf = temp; }
No special assignment operator is needed here.
Correct me if I am wrong.
V
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:37yUb.1814 15$Rc4.1339653@ attbi_s54... "Jonathan Turkanis" <te******@kanga roologic.com> wrote...
Why? 'pf = pd' expression will result into the use of compiler- generated assignment operator and a construction of a temporary on the right side, no? It should be equivalent to
{ Vector<float> temp(pd); pf = temp; }
No special assignment operator is needed here.
Correct me if I am wrong.
You're not -- I was :(.
Jonathan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Oplec |
last post by:
Hi,
I'm learning C++ as a hobby using The C++ Programming Language : Special
Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that
deal with templates. Exercise 13.9 asks for me to turn a previously
made String class that deals with char's into a templated String class
that uses the template parameter C instead of char. I thought it would
be fairly simple to do this exercise, but I encoutered many errors for
my...
|
by: Ben Ingram |
last post by:
Hi all,
I am writing a template matrix class in which the template parameters are
the number of rows and number of columns. There are a number of reasons
why this is an appropriate tradeoff for my particular application. One of the
advantages is that the _compiler_ can force inner matrix dimensions used in
multiplication to agree. A _complie-time_ error will be triggered if you
write A * B and the number of coluns in A does not equal the...
|
by: Dave Corby |
last post by:
Hi all,
I have an overloaded template function, and in one particular spot can't
get the right version of it to be called. Everywhere else in the program
the correct version is called. Here's the function declarations:
template <class NumT, class ExpT>
NumT rppower (NumT const x, ExpT const y);
template <class NumT, class ExpT>
SafeInt<NumT> rppower (SafeInt<NumT> const x, SafeInt<ExpT> const y);
|
by: Chris |
last post by:
I am having a very strange problem involving virtual
functions in template classes. First of all, here is an
extremely simplified structure of the two classes I am
having problems with.
template<class Type> class base
{
public:
base& operator/=(const base&);
Type *image;
|
by: David Williams |
last post by:
Hi all,
I have a templated Vector3D class which holds (x,y,z) components as the
specified type. I quite often wish to cast a Vector3D holding ints into
a Vector3D holding floats and vice versa. Like so:
Vector3D<int> intVec(10,20,30);
Vector3D<float> floatVec = intVec;
Of course this doesn't work. I would be happy if instead the following
| |
by: danilo.horta |
last post by:
Hi folks
I'm having a scope resolution issue. The gnu compiler is trying to
use the "operator function" from derived class rather than from correct
one, the base class.
// VecBasis.h file
template<class T, size_t numDim> class VecBasis {
protected:
|
by: mast2as |
last post by:
Is it possible to limit a template class to certain types only. I found
a few things on the net but nothing seems to apply at compile time.
template <typename T>
class AClass
{
public:
AClass() {}
};
|
by: woessner |
last post by:
Hi all,
I whipped up a quick class to represent a matrix for use with LAPACK.
It's a template class so it can support the 4 data types supported by
LAPACK (single/double x complex/real). I added a conversion operator
to automatically convert the object to a pointer of the appropriate
type. This makes using LAPACK in C++ a lot easier.
Unfortunately, it does not work well for the complex data types. The
reason is that LAPACK (or,...
|
by: Hamilton Woods |
last post by:
Diehards,
I developed a template matrix class back around 1992 using Borland C++ 4.5
(ancestor of C++ Builder) and haven't touched it until a few days ago. I
pulled it from the freezer and thawed it out. I built a console app using
Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the
header file had to be commented out.
I built a console app using Borland C++ Builder 5. The linker complained of
references to...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |