473,387 Members | 3,033 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,387 software developers and data experts.

Question about objects

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<Basearg) {}

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

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

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

Aug 28 '07 #1
27 1595
R2D2 wrote:
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
Aug 28 '07 #2
On 2007-08-28 19:13:17 -0400, "Default User" <de***********@yahoo.comsaid:
R2D2 wrote:
>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.
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.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 28 '07 #3
LR
R2D2 wrote:
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{};
Inherited is a kind of Base;
http://www.parashift.com/c++-faq-lit....html#faq-19.3

But

std::vector<Inheritedis not a std::vector<Base>
http://www.parashift.com/c++-faq-lit....html#faq-21.3

LR
Aug 29 '07 #4
R2D2 wrote:
>
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<Basearg) {}

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

// does work
std::vector<Basev2;
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++

--
pete
Aug 29 '07 #5
R2D2 wrote:
>
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<Basearg) {}

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

// does work
std::vector<Basev2;
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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 29 '07 #6
Default User said:
R2D2 wrote:
>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++,
Actually, it isn't...
and this is a C newsgroup.
....and this is /also/ a C++ newsgroup, thanks to a broken crosspost.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 29 '07 #7
Richard Heathfield wrote:
Default User said:
>R2D2 wrote:
>>>
could someone please explain to me, why in the
following code the second approach does not work?

#include <vector>

class Base {};

Because it's C++,

Actually, it isn't...
>and this is a C newsgroup.

...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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 29 '07 #8
I don't really follow this - could you give a fuller explanation?

On 29 Aug 2007 at 0:01, LR wrote:
R2D2 wrote:
>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{};

Inherited is a kind of Base;
http://www.parashift.com/c++-faq-lit....html#faq-19.3

But

std::vector<Inheritedis not a std::vector<Base>
http://www.parashift.com/c++-faq-lit....html#faq-21.3

LR

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

On 29 Aug 2007 at 8:55, CBFalconer wrote:
Richard Heathfield wrote:
>Default User said:
>>R2D2 wrote:

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

#include <vector>

class Base {};

Because it's C++,

Actually, it isn't...
>>and this is a C newsgroup.

...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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Aug 29 '07 #10
R2D2 wrote:
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
Aug 29 '07 #11
On Aug 29, 9:24 pm, R2D2 <r...@spamtrap.invalidwrote:
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
<stdin>:10: warning: label `std' defined but not used

On 29 Aug 2007 at 8:55, CBFalconer wrote:
Richard Heathfield wrote:
Default User said:
R2D2 wrote:
>>could someone please explain to me, why in the
following code the second approach does not work?
>>#include <vector>
>>class Base {};
>Because it's C++,
Actually, it isn't...
>and this is a C newsgroup.
...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.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Aug 29 '07 #12
On Aug 29, 9:24 pm, R2D2 <r...@spamtrap.invalidwrote:
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.

Aug 29 '07 #13
LR
R2D2 wrote:

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

On 29 Aug 2007 at 0:01, LR wrote:
>R2D2 wrote:
>>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{};
Inherited is a kind of Base;
http://www.parashift.com/c++-faq-lit....html#faq-19.3

But

std::vector<Inheritedis not a std::vector<Base>
http://www.parashift.com/c++-faq-lit....html#faq-21.3
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<Inheritedis not a child class of X<Baseand does not inherit from
X<Base>

Compare this with Y<Inheritedand Y<Basebelow.
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<Inheritedxi;
X<Base*pxb = &xi; // not ok, won't compile
X<Base&xb = xi; // not ok, won't compile

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

LR
Aug 29 '07 #14
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++.

On 29 Aug 2007 at 20:37, J. J. Farrell wrote:
On Aug 29, 9:24 pm, R2D2 <r...@spamtrap.invalidwrote:
>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.
Aug 29 '07 #15
On 2007-08-30 00:17, R2D2 wrote:
On 29 Aug 2007 at 20:37, J. J. Farrell wrote:
>On Aug 29, 9:24 pm, R2D2 <r...@spamtrap.invalidwrote:
>>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.
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).

--
Erik Wikström
Aug 29 '07 #16
On Aug 30, 10:17 am, R2D2 <r...@spamtrap.invalidwrote:
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?

Aug 29 '07 #17
R2D2 <r2**@spamtrap.invalidwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 29 '07 #18
On Aug 30, 8:23 am, R2D2 <r...@spamtrap.invalidwrote:
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."

Aug 29 '07 #19
R2D2 wrote:
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++.
*plonk*

Brian
Aug 29 '07 #20
R2D2 wrote:
>
I posted to comp.lang.c in case people could see a problem with the
underlying C code - please dont remove this crosspost. Thanks.
OK.
Here's what's wrong with your program
from a C point of view:
There isn't any part of your program
that means anything in C.

--
pete
Aug 29 '07 #21
Old Wolf wrote:
On Aug 30, 8:23 am, R2D2 <r...@spamtrap.invalidwrote:
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."
Scrap it.


Brian
Aug 29 '07 #22
On Aug 29, 10:59 am, R2D2 <r...@spamtrap.invalidwrote:
could someone please explain to me, why in the
following code the second approach does not work?

class Base {};
class Inherited : public Base{};
void foo(std::vector<Basearg) {}

void main() {
Should be:
int main() {
std::vector<Inheritedv1;

// doesn't work
foo(v1);
There is no implicit conversion from vector<Inheritedto
vector<Base>.

Aug 29 '07 #23
R2D2 wrote, On 29/08/07 23:17:
Do you understand what C++ is?
Lots of people here do. It is a language.
It's the C language, with a few
extensions bolted on like objects and polymorphism.
No, it is a different language. A number of things that are legal C are
illegal C++.
So in any
malfunctioning code, the problem might be the C base code, or the C++
extension parts.
No, because there is no "C base code" only C++ code when you are writing
C++.
So it's perfectly suitable to ask in comp.lang.c as
well as comp.lang.c++.
What makes you think the people in comp.lang.c++ don't know all of the
"C base code?"

No, it is not reasonable to ask in comp.lang.c about C++ code, and since
you know about comp.lang.c++ you have no good excuse for doing so.
On 29 Aug 2007 at 20:37, J. J. Farrell wrote:
<snip>

Don't top post. Your reply belongs under or interspersed with the
message you are replying to.

Oh, and don't bother to reply. *PLONK*.
--
Flash Gordon
Aug 30 '07 #24
R2D2 wrote: *** and top-posted - fixed ***
On 29 Aug 2007 at 0:01, LR wrote:
>R2D2 wrote:
>>>
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{};

Inherited is a kind of Base;
http://www.parashift.com/c++-faq-lit....html#faq-19.3

But
std::vector<Inheritedis not a std::vector<Base>
http://www.parashift.com/c++-faq-lit....html#faq-21.3

I don't really follow this - could you give a fuller explanation?
It is not C. Please keep this out of c.l.c. F'ups set.

Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. I fixed this one. See the following links:

--
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/ (taming google)
<http://members.fortunecity.com/nnqweb/ (newusers)

--
Posted via a free Usenet account from http://www.teranews.com

Aug 30 '07 #25


Why is it that you are asking people in this NG for something, but, when
they ask you for something (NOT to top-post), you throw mud in their face?

**plonk**

fool
Aug 30 '07 #26
On 2007-08-29 19:09:49 -0400, pete <pf*****@mindspring.comsaid:
R2D2 wrote:
>>
I posted to comp.lang.c in case people could see a problem with the
underlying C code - please dont remove this crosspost. Thanks.

OK.
Here's what's wrong with your program
from a C point of view:
There isn't any part of your program
that means anything in C.
So why is this cross-posted to comp.lang.c++?

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 30 '07 #27
On Thu, 30 Aug 2007 00:17:40 +0200 (CEST), R2D2
<r2**@spamtrap.invalidwrote:
>Do you understand what C++ is?
Yes. However, apparently you don't. Whatever you imagine to be the "C
base code" is surely understood by the experts who contribute to
c.l.c++.

And please stop top-posting.
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++.

On 29 Aug 2007 at 20:37, J. J. Farrell wrote:
>On Aug 29, 9:24 pm, R2D2 <r...@spamtrap.invalidwrote:
>>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.
--
Al Balmer
Sun City, AZ
Aug 30 '07 #28

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

Similar topics

11
by: thechaosengine | last post by:
Hi all, I have a very general but quite significant question about objects. My question is, when should I create them? I know thats a crap question so let me explain a bit further. Lets...
9
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
6
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. A snippet is worth 1000 words (: so... class Base { }; class Derived1:public Base...
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
3
by: Miguel | last post by:
Hi all friends: It's said that Sessions objects in ASP 3.0 with IIS 5.0 occupy certain memory of the machine which take to take care about use a lot of Sessions objects in the ASPs pages of the...
2
by: headware | last post by:
I am new to 3-tiered design and am in the process of designing an application that will work with ASP.NET as well as Windows Forms (and possibly with a PDA of some sort down the road). My question...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
27
by: SasQ | last post by:
Hello. I wonder if literal constants are objects, or they're only "naked" values not contained in any object? I have read that literal constants may not to be allocated by the compiler. If the...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
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,...

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.