Question about objects

R

R2D2

Hi,

could someone please explain to me, why in the
following code the second approach does not work?

Thanks for your help.


#include <vector>

class Base {};

class Inherited : public Base{};

void foo(std::vector<Base> arg) {}

void main() {
std::vector<Inherited> v1;
v1.push_back(Inherited());

// does work
std::vector<Base> v2;
for(int i=0; i<v1.size(); v2.push_back(v1[i++]));
foo(v2);

// doesn't work
foo(v1);
}
 
D

Default User

R2D2 said:
Hi,

could someone please explain to me, why in the
following code the second approach does not work?

Thanks for your help.


#include <vector>

class Base {};

Because it's C++, and this is a C newsgroup.

Try comp.lang.c++. When you post it there, change the return type of
main() to int, and explain what you mean by "does not work".





Brian
 
P

Pete Becker

Because it's C++, and this is a C newsgroup.

For some unspecified value of "this". I don't read comp.lang.c, but I
do read comp.lang.c++, and that's where I'm reading this message.
 
C

CBFalconer

R2D2 said:
could someone please explain to me, why in the
following code the second approach does not work?

#include <vector>

class Base {};

class Inherited : public Base{};

void foo(std::vector<Base> arg) {}

void main() {
std::vector<Inherited> v1;
v1.push_back(Inherited());

// does work
std::vector<Base> v2;
for(int i=0; i<v1.size(); v2.push_back(v1[i++]));
foo(v2);

// doesn't work
foo(v1);
}

This is C++, not C. F'ups set.
 
P

pete

R2D2 said:
Hi,

could someone please explain to me, why in the
following code the second approach does not work?

Thanks for your help.

#include <vector>

class Base {};

class Inherited : public Base{};

void foo(std::vector<Base> arg) {}

void main() {
std::vector<Inherited> v1;
v1.push_back(Inherited());

// does work
std::vector<Base> v2;
for(int i=0; i<v1.size(); v2.push_back(v1[i++]));
foo(v2);

// doesn't work
foo(v1);
}

Not in comp.lang.c. No. Absolutely not.

Followup To: comp.lang.c++
 
C

CBFalconer

Richard said:
Default User said:

Actually, it isn't...


...and this is /also/ a C++ newsgroup, thanks to a broken crosspost.

It won't be, if people observe the F'up I have set.
 
R

R2D2

I posted to comp.lang.c in case people could see a problem with the
underlying C code - please dont remove this crosspost. Thanks.
 
D

Default User

R2D2 said:
I posted to comp.lang.c in case people could see a problem with the
underlying C code - please dont remove this crosspost. Thanks.

It doesn't belong here. There is NO underlying C code. There is C++
code that is compatible with C. People in comp.lang.c++ are perfectly
able to answer it.

You are off-topic in comp.lang.c, and you aren't going to get many good
answers.

Also:

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>




Brian
 
F

Francine.Neary

I posted to comp.lang.c in case people could see a problem with the
underlying C code - please dont remove this crosspost. Thanks.

Please don't top-post.

I found plenty of problems with the "underlying C code":

<stdin>:1:18: vector: No such file or directory
<stdin>:3: error: syntax error before "Base"
<stdin>:3: error: syntax error before '{' token
<stdin>:3: warning: ISO C does not allow extra `;' outside of a
function
<stdin>:5: error: syntax error before "Inherited"
<stdin>:5: warning: ISO C does not allow extra `;' outside of a
function
<stdin>:7: error: syntax error before ':' token
<stdin>:9: warning: return type of `main' is not `int'
<stdin>: In function `main':
<stdin>:10: error: syntax error before ':' token
<stdin>:11: error: `v1' undeclared (first use in this function)
<stdin>:11: error: (Each undeclared identifier is reported only once
<stdin>:11: error: for each function it appears in.)
<stdin>:11: warning: implicit declaration of function `Inherited'
<stdin>:13: error: syntax error before '/' token
<stdin>:15: error: `for' loop initial declaration used outside C99
mode
<stdin>:15: error: `v2' undeclared (first use in this function)
<stdin>:18: error: syntax error before '/' token
<stdin>:18:11: missing terminating ' character
<stdin>:18:11: warning: character constant too long for its type
It won't be, if people observe the F'up I have set.
 
J

J. J. Farrell

I posted to comp.lang.c in case people could see a problem with the
underlying C code - please dont remove this crosspost. Thanks.

What on earth are you talking about? What do you mean by "underlying C
code"? No-one in comp.lang.c can usefully comment if your post is not
about C, and there was nothing even vaguely related to C in your post.
 
G

Guest

>
I don't really follow this - could you give a fuller explanation?


Please don't top-post, and also don't quote signatures.

Simply put it's like this, std::vector<Base> is a type, just like int
and double. std::vector<Inherited> is also a type, and while there might
be some relation between Base and Inhertited, there's no relation
between std::vector<Base> and std::vector<Inhertited>. One reason for
this is that std::vector<Base> stores objects of type Base, and to
insert an object into the vector you need to copy it, and if you copy a
object of type Inhertited to a variable of type Base you get slicing
(look it up if you don't know what it means). In short, this means that
the objects stored in a vector (or any other container) can't act
polymorphic.
 
L

LR

R2D2 wrote:

Your top posting has been moved. This will not be posted in comp.lang.c.

> I don't really follow this - could you give a fuller explanation?

I'll take a stab at it, but I suggest that you read the faq and ask a
more specific question.

Also, please don't post your follow up to comp.lang.c. This doesn't
belong there.


Below, Inherited is a child class of Base and inherits from Base.
X<Inherited> is not a child class of X<Base> and does not inherit from
X<Base>

Compare this with Y<Inherited> and Y<Base> below.


class Base {
public:
virtual ~Base() {}
};

class Inherited : public Base {};

template<typename T>
class X {};

template<typename T>
class Y : public Base {}

int main() {

// ok
Inherited i;
Base *pb = &i;
Base &b = i;


X<Inherited> xi;
X<Base> *pxb = &xi; // not ok, won't compile
X<Base> &xb = xi; // not ok, won't compile

// ugly, but both Y<Inherited> and Y<Base>
// inherit from Base
// It's just an example.
Y<Inherited> yi;
Y<Base> yb;
Base &pyi = yi;
Base &pyb = yb;


}



LR
 
R

R2D2

Do you understand what C++ is? It's the C language, with a few
extensions bolted on like objects and polymorphism. So in any
malfunctioning code, the problem might be the C base code, or the C++
extension parts. So it's perfectly suitable to ask in comp.lang.c as
well as comp.lang.c++.
 
G

Guest

Do you understand what C++ is? It's the C language, with a few
extensions bolted on like objects and polymorphism. So in any
malfunctioning code, the problem might be the C base code, or the C++
extension parts. So it's perfectly suitable to ask in comp.lang.c as
well as comp.lang.c++.

Please don't top-post, see the FAQ, section 5, for more information
about how to format your replies.
http://www.parashift.com/c++-faq-lite/how-to-post.html

His understanding of what C++ is is better than yours. While what you
said was true once upon a time this is no longer the case. When BS
created C++ it started out as an extension to the C language, but since
then both languages have evolved, and not always in the same direction.
The result of this evolution is that there are now lots of valid C++
code that is also valid C code, however there is an ever greater amount
of C++ code that is *not* valid C. Likewise there are now lots of valid
C code that is also valid C++, and also much valid C that is *not* valid
C++.

An it's not just the classes and templates, there is code that is both
valid C and valid C++ that does not have the exact same meaning, which
can cause a lot of problems.

More to the point, of the code you posted in the original message not a
single line would have compiles or been meaningful in C, but in C++
there was only one slight error (except for the problem your question
was about).
 
O

Old Wolf

Do you understand what C++ is? It's the C language, with a few
extensions bolted on like objects and polymorphism.

Not true (and off-topic to discuss)
So in any malfunctioning code, the problem might be the
C base code, or the C++ extension parts.

Please post just the "C base code" parts of your program.
Then we can tell you if anything is wrong with that part.
So it's perfectly suitable to ask in comp.lang.c as
well as comp.lang.c++.

Well, C++ is just Java with a few libraries missing and
slightly altered syntax. Why not ask in comp.lang.java too?
 
K

Keith Thompson

R2D2 said:
Do you understand what C++ is? It's the C language, with a few
extensions bolted on like objects and polymorphism. So in any
malfunctioning code, the problem might be the C base code, or the C++
extension parts. So it's perfectly suitable to ask in comp.lang.c as
well as comp.lang.c++.

Most of us here understand what C++ is better than you do.

C and C++ are two distinct languages. C++ happens to contain most of
the features of C as a subset, but those features are *part of C++*.
If I write
int x = 42;
it could be valid C or valid C++. If I write it as part of a C++
program, it's C++, and the folks in comp.lang.c++ are perfectly
competent to discuss it. They don't need to run to the C experts to
ask about features of their own language.

And again, please stop top-posting. Read the following:

http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php

I've redirected followups to comp.lang.c++, where this entire thread
should have been in the first place.
 
O

Old Wolf

I don't really follow this - could you give a fuller explanation?

"This artoo unit is malfunctioning. Top-post switch
is stuck in the 'on' position. Request assistance."
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top