Question on templates

I

imanpreet

Hi,

Could anyone proivde a logical argument against this

class Shape
{

};


class Circle:public Shape
{

};

void operator=(std::vector<Shape> sh, std::vector<Circle> cir)
{

}

std::vector<Shape> sh;
std::vector<circle> cir;

sh = cir;
 
R

red floyd

Hi,

Could anyone proivde a logical argument against this

class Shape
{

};


class Circle:public Shape
{

};

void operator=(std::vector<Shape> sh, std::vector<Circle> cir)
{

}

std::vector<Shape> sh;
std::vector<circle> cir;

sh = cir;

Yeah, sh contains none of the Circle specific stuff, including the lack
of any Circle specific data and virtual member functions.
 
A

Andrey Tarasevich

...
Could anyone proivde a logical argument against this

class Shape
{

};


class Circle:public Shape
{

};

void operator=(std::vector<Shape> sh, std::vector<Circle> cir)
{

}

std::vector<Shape> sh;
std::vector<circle> cir;

sh = cir;
...

Against what? What is the point of this code sketch?
 
B

Bruce Trask

Well, first and foremost, it won't compiler due to the fact that operator=
must be a non static member function.
Apart from that, what I think you are trying to do will do nothing but
shave the Circle parts off of a bunch of Circle Shapes and store them in in
a vector of Shapes.
Not sure why you would want to do this. What problem are you trying to
solve here?

Regards,
Bruce
 
M

Minti

Bruce said:
Well, first and foremost, it won't compiler due to the fact that operator=
must be a non static member function.
Apart from that, what I think you are trying to do will do nothing but
shave the Circle parts off of a bunch of Circle Shapes and store them in in
a vector of Shapes.
Not sure why you would want to do this. What problem are you trying to
solve here?


Obviously it won't compile, the reason I am asking it why should it
NOT. I don't see any logical difficulty in having a vector of Circles
to be assigned to a vector of Shapes. In Chapter 13 Of TC++PL, bs
argues that when using refrences this should not work because you are
in a sense modifying the actual Circle Object, which is not the case
here.
 
B

Bruce Trask

Imanpreet,

The only reason it won't compile is that operator= must be a non static
member. Is your question why is operator= constrained to being only a non
static member?

What will compile is the following but again, you will get slicing.

#include <vector>
#include <algorithm>

using namespace std;

class Shape
{

};

class Circle : public Shape
{
};

int main()
{
vector<Circle> myCircleVec(5);
vector<Shape> myShapeVec(5);

copy(myCircleVec.begin(), myCircleVec.end(), myShapeVec.begin());
// myShapeVec = myCircleVec; // won't compiler
}

Is that what you are trying to do?

Hope that helps,
Bruce
 
R

Rolf Magnus

Minti said:
Obviously it won't compile, the reason I am asking it why should it
NOT.

Because an assignment operator has to be a member function of the right hand
operand's class. Yours isn't, so it won't compile.
I don't see any logical difficulty in having a vector of Circles
to be assigned to a vector of Shapes.

I think you should read up on polymorphism. As long as you store instances
in your vectors instead of pointers, you won't be able to store your
Circles in a vector<Shape>. You will only store the Shape part of each
Circle, losing the most important parts.
 
R

Rolf Magnus

Rolf said:
Because an assignment operator has to be a member function of the right
hand operand's class.

This of course must be "the left hand operand's class".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,298
Messages
2,571,540
Members
48,275
Latest member
tetedenuit01

Latest Threads

Top