473,385 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

can I use base class pointer to swap the derived object in a vector?

Hi,
I have 2 vectors, vector<Derived_class*> vec1, vec2; can I do the
following,

for (int i=0; i<vec1.size(); ++i){
for (int j=0; j<vec2.size(); ++j){
if (.. some comparision of vec1[i] and vec2[j]...){
Base_Class b = vec2[i];
vec2[i] = vec2[j];
vec2[j] = b;
}
}
}

Is there any problem here? In swapping I do not cast the pointer.

Jul 22 '05 #1
3 1535
On Fri, 02 Jul 2004 10:41:34 -0600, John Black <bl***@eed.com> wrote:
Hi,
I have 2 vectors, vector<Derived_class*> vec1, vec2; can I do the
following,

for (int i=0; i<vec1.size(); ++i){
for (int j=0; j<vec2.size(); ++j){
if (.. some comparision of vec1[i] and vec2[j]...){
Base_Class b = vec2[i];
vec2[i] = vec2[j];
vec2[j] = b;
}
}
}

Is there any problem here? In swapping I do not cast the pointer.


You question doesn't make a lot of sense. The code you quote doesn't
compile, so no you can't do that. But why not do it like this?
if (.. some comparision of vec1[i] and vec2[j]...){
Derived_class* b = vec2[i];
vec2[i] = vec2[j];
vec2[j] = b;
}

It seems the obvious thing to do.

john
Jul 22 '05 #2

"John Black" <bl***@eed.com> wrote in message
news:40***************@eed.com...
Hi,
I have 2 vectors, vector<Derived_class*> vec1, vec2; can I do the
following,

for (int i=0; i<vec1.size(); ++i){
for (int j=0; j<vec2.size(); ++j){
if (.. some comparision of vec1[i] and vec2[j]...){
Base_Class b = vec2[i];
I take it you mean

Base_Class* b = vec2[i];

?
vec2[i] = vec2[j];
vec2[j] = b;
}
}
}

Is there any problem here? In swapping I do not cast the pointer.


Assuming that's what you mean, it's fine.

-Howard

Jul 22 '05 #3
Howard wrote:
"John Black" <bl***@eed.com> wrote in message
news:40***************@eed.com...
Hi,
I have 2 vectors, vector<Derived_class*> vec1, vec2; can I do the
following,

for (int i=0; i<vec1.size(); ++i){
for (int j=0; j<vec2.size(); ++j){
if (.. some comparision of vec1[i] and vec2[j]...){
Base_Class b = vec2[i];

I take it you mean

Base_Class* b = vec2[i];

?

vec2[i] = vec2[j];
vec2[j] = b;
}
}
}

Is there any problem here? In swapping I do not cast the pointer.

Assuming that's what you mean, it's fine.


Somehow I think that conversion between Base_Class* (which is what
'b' is) and Derived_class* (which is what 'vec2[j]' is), requires
a cast (a static_cast, for example).

But, I am still not sure why not use 'std::swap'?

std::swap(vec2[i], vec2[j]);

all type considerations are hidden and taken care of.

Victor
Jul 22 '05 #4

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

Similar topics

2
by: CD | last post by:
Is this possible: class base; class derived; //:public base vector <base*> bList; vector<derived*> dList; //add some derived class pointer entries to dList;
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
15
by: Simon | last post by:
Hi, If I have two classes. Class A { public: A( const char *sz); ~A(); virtual void Foo() = 0;
10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
16
by: call_me_anything | last post by:
why is the following not allowed : vector <Base *vec_of_base; vector <Derived *vec_of_derived; vec_of_base = vec_of_derived; Note : The following is allowed :
3
by: nw | last post by:
Hi, I have three classes, a template pure virtual base class, a template derived class and a third which I would like to use to store copies of the derived class. The code looks like this: ...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
2
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual...
12
by: subramanian100in | last post by:
Suppose class Base { public: virtual ~Test() { ... } // ... }; class Derived : public Base
10
by: Dom Jackson | last post by:
I have a program which crashes when: 1 - I use static_cast to turn a base type pointer into a pointer to a derived type 2 - I use this new pointer to call a function in an object of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
jinu1996
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...

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.