C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Vector to array with custom objects

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Ron Olson
Guest





PostPosted: Sat Oct 23, 2004 2:21 pm    Post subject: Vector to array with custom objects Reply with quote



Hi all-

I have a class Foo that have an overloaded operator int().:

class Foo
{
public:

Foo(int x) : m_Num(x)
{
}

~Foo()
{
}

void PrintNum(void)
{
cout << "I'm object " << m_Num << endl;
}

operator int()
{
return m_Num;
}

private:

int m_Num;
};

and this works when passing to functions like:

Foo myFoo(6);
NoFun(myFoo);

void NoFun(int blah)
{
cout << blah << endl;
}

Now if I have a vector of Foos:

vector
I can still call NoFun() with:

NoFun(MyFoos[0]); // assume we've put a Foo in the vector

Okay, great.

What I'm stumped on is how to pass a vector of Foos to a function that
wants a pointer to a list of ints:

void Fun(int NumInts, int* Ints)
{

}

which I want to call with:

Fun(MyFoos.size(), &MyFoos[0]);

which in turn gives me Error : function call 'Fun2(unsigned long, Foo
*)' does not match
'Fun2(int, const int *)'
HelloWorld.cp line 74 Fun2(Foos2.size(), &Foos2[0]); // no good

from CodeWarrior 8.


What I'm not sure of is why the operator int() doesn't allow the vector
to be passed as an array of ints, instead of Foos. Though we are passing
the vector as an array, why doesn't the compiler see the operator int()
in this situation? I presume it's because we're passing the vector to a
C-style array, where all the ints have to be there as we're no longer
dealing with objects, just an array of ints, and the vector doesn't do
what I presume is memory magic to make it seem more like an array (I
would imagine that an implementation would be to copy the vector to a
new one, calling on the operator int() and then passing the "copy" to
the function.

On the other hand, maybe this is legal and I'm just doing it wrong. Any
suggestions would be appreciated.


Thanks,

Ron

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Frank Birbacher
Guest





PostPosted: Sun Oct 24, 2004 2:45 pm    Post subject: Re: Vector to array with custom objects Reply with quote



Hi!

Ron Olson wrote:
Quote:
and the vector doesn't do
what I presume is memory magic to make it seem more like an array (I
would imagine that an implementation would be to copy the vector to a
new one, calling on the operator int() and then passing the "copy" to
the function.

Yes, you are correct here. The vector is not supposed to do the
conversion istelf. You will have to construct a new array:

std::vector<Foo> foovec;
std::vector<int> intvec(foovec.begin(), foovec.end());
Fun(intvec.size(), &intvec[0]);

Quote:
Any
suggestions would be appreciated.

Maybe you change function 'Fun' to take a vector instead.

Frank


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ivan Vecerina
Guest





PostPosted: Sun Oct 24, 2004 2:50 pm    Post subject: Re: Vector to array with custom objects Reply with quote



"Ron Olson" <olsonr (AT) panix (DOT) com> wrote

Quote:
I have a class Foo that have an overloaded operator int().:

class Foo
{
public:
.....
private:
int m_Num;
};
....
Now if I have a vector of Foos:

vector<Foo> MyFoos;
....
What I'm stumped on is how to pass a vector of Foos to a function that
wants a pointer to a list of ints:

void Fun(int NumInts, int* Ints)
{

}

which I want to call with:

Fun(MyFoos.size(), &MyFoos[0]);

No, this won't work - and shouldn't.
The problem is that sizeof(Foo) is not guaranteed to be the
same as sizeof(int), so a Foo[] cannot be handled as an int[]
(potentially different memory layout, as you expected).

In practice, you may get this to work using reinterpret_cast<int*>,
but formally this is Undefined Behavior, and therefore not portable.

Quote:
What I'm not sure of is why the operator int() doesn't allow the vector
to be passed as an array of ints, instead of Foos. Though we are passing
the vector as an array, why doesn't the compiler see the operator int()
in this situation? I presume it's because we're passing the vector to a
C-style array, where all the ints have to be there as we're no longer
dealing with objects, just an array of ints, and the vector doesn't do
what I presume is memory magic to make it seem more like an array (I
would imagine that an implementation would be to copy the vector to a
new one, calling on the operator int() and then passing the "copy" to
the function.
In C++, such copies do not happen implicitly.

But you could easily make this as follows:
vector<int> myInts( MyFoos.begin(), MyFoos.end() );
Fun(MyFoos.size(), &MyFoos[0]);


I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Richter
Guest





PostPosted: Tue Oct 26, 2004 4:15 am    Post subject: Re: Vector to array with custom objects Reply with quote

Hi,

Quote:
I have a class Foo that have an overloaded operator int().:

class Foo
{

/* snip */

Quote:
operator int()
{
return m_Num;
}

};

What I'm stumped on is how to pass a vector of Foos to a function that
wants a pointer to a list of ints:

void Fun(int NumInts, int* Ints)
{

}

which I want to call with:

Fun(MyFoos.size(), &MyFoos[0]);

which in turn gives me Error : function call 'Fun2(unsigned long, Foo
*)' does not match
'Fun2(int, const int *)'
HelloWorld.cp line 74 Fun2(Foos2.size(), &Foos2[0]); // no good

Indeed, no good.

Quote:
What I'm not sure of is why the operator int() doesn't allow the vector
to be passed as an array of ints, instead of Foos.

This is due to the way how "C-style" arrays work in C++. They're just a
collection of elements of the indicated type, placed near to each other
in memory. That is, if you pass an int[] to a function, the compiler
for this function expects that it can reach the next element of the
array by adding sizeof(int) bytes to the address of the first object.
However, you're passing an array of "class Foo", and it is not at all
given that sizeof(int) == sizeof(Foo).

The solution to this problem is quite obvious, IMHO. Don't write
Fun() as a function that accepts an (int *), but rather make this
a function template of a given vector type.

Quote:
Though we are passing
the vector as an array, why doesn't the compiler see the operator int()
in this situation?

Because the contract required by a function operating on int arrays implies
more than just a conversion to int. It also requires the condition that
you can get from element to element via sizeof(int) bytes. And that is not
given.

Quote:
I presume it's because we're passing the vector to a
C-style array, where all the ints have to be there as we're no longer
dealing with objects, just an array of ints, and the vector doesn't do
what I presume is memory magic to make it seem more like an array (I
would imagine that an implementation would be to copy the vector to a
new one, calling on the operator int() and then passing the "copy" to
the function.

On the other hand, maybe this is legal and I'm just doing it wrong. Any
suggestions would be appreciated.

No, it's not legal.

So long,
Thomas


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.