Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 11th, 2006, 07:25 PM
ken.carlino@gmail.com
Guest
 
Posts: n/a
Default Create a Reference stl:vector attribute

>From http://www.parashift.com/c++-faq-lit...s.html#faq-8.6, it
said "Use references when you can, and pointers when you have to."

And I need to convert this java class to c++:

public class Y {
public final int[] w

public Y(final List alist {
w = new int[alist.size()];
}
}

So I am thinking about create a Reference of stl:vector for my
attribute 'w'.

class Y
{
public:
Y
virtual ~Y();

vector<int>& w;
};

and in my constructor, i initialize it like this:
Y::Y(list<Z>& alist) :
w ( vector<int>(alist.size()) )
{

}

And I get this compilation error:
.../src/YMapData.cpp:9: error: invalid initialization of non-const
reference of type 'std::vector<int, std::allocator<int> >&' from a
temporary of type 'std::vector<int, std::allocator<int> >'

I appreciate if someone can tell me what did I do wrong.

  #2  
Old January 11th, 2006, 07:45 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Create a Reference stl:vector attribute

ken.carlino@gmail.com wrote:[color=blue][color=green]
>>From http://www.parashift.com/c++-faq-lit...s.html#faq-8.6, it[/color]
> said "Use references when you can, and pointers when you have to."[/color]

That's to distinguish between pointers and reference. In your case, you
simply need an object. I understand, coming from Java some folks do not
grasp that concept sometimes. You need to make an effort.
[color=blue]
> And I need to convert this java class to c++:
>
> public class Y {
> public final int[] w
>
> public Y(final List alist {
> w = new int[alist.size()];
> }
> }
>
> So I am thinking about create a Reference of stl:vector for my
> attribute 'w'.
>
> class Y
> {
> public:
> Y
> virtual ~Y();
>
> vector<int>& w;[/color]

Should simply be

vector<int> w;
[color=blue]
> };
>
> and in my constructor, i initialize it like this:
> Y::Y(list<Z>& alist) :
> w ( vector<int>(alist.size()) )[/color]

Should instead be

w(alist.size())
[color=blue]
> {
>
> }
>
> And I get this compilation error:
> ../src/YMapData.cpp:9: error: invalid initialization of non-const
> reference of type 'std::vector<int, std::allocator<int> >&' from a
> temporary of type 'std::vector<int, std::allocator<int> >'
>
> I appreciate if someone can tell me what did I do wrong.
>[/color]

You will need to learn to _instantiate_ objects.

V
  #3  
Old January 11th, 2006, 07:55 PM
ken.carlino@gmail.com
Guest
 
Posts: n/a
Default Re: Create a Reference stl:vector attribute

Thanks.

If I change it to this:

class Y
{
public:
Y
virtual ~Y();

vector<int> w;

Do I still need to free it in Y's destructor? if yes, how? I don't do
'delete w', right since 'w' is not a pointer or reference?

And since w is a public attribute, can otherside still access it?
can a caller do this?

Y y;
cout << y.w.size() << endl;
y.w.pushback(6);
cout << y.w.size() << endl;

  #4  
Old January 11th, 2006, 08:15 PM
W Marsh
Guest
 
Posts: n/a
Default Re: Create a Reference stl:vector attribute

On 11 Jan 2006 11:48:19 -0800, ken.carlino@gmail.com wrote:
[color=blue]
>Thanks.
>
>If I change it to this:
>
> class Y
> {
> public:
> Y
> virtual ~Y();
>
> vector<int> w;[/color]

Why do you declare your constructors like that? It simply will not
compile. Try:

class Y
{
public:
Y();
virtual ~Y();
/* ... */
};
[color=blue]
>
>Do I still need to free it in Y's destructor? if yes, how? I don't do
>'delete w', right since 'w' is not a pointer or reference?[/color]

No, the data isn't dynamically allocated, so it doesn't need to be
dynamically de-allocated.
[color=blue]
>And since w is a public attribute, can otherside still access it?[/color]

Yes, but having public data in classes isn't particularly good OOP
design.
  #5  
Old January 11th, 2006, 08:15 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Create a Reference stl:vector attribute

ken.carlino@gmail.com wrote:[color=blue]
> If I change it to this:
>
> class Y
> {
> public:
> Y[/color]

???
[color=blue]
> virtual ~Y();
>
> vector<int> w;
>
> Do I still need to free it in Y's destructor?[/color]

No. The destruction of a Y will cause the destruction of any members
of the Y, and the w will be freed automagically.
[color=blue]
> if yes, how? I don't do
> 'delete w', right since 'w' is not a pointer or reference?[/color]

It's not, so it will destroyed without your attention.
[color=blue]
> And since w is a public attribute, can otherside still access it?[/color]

Yes. If you're concerned with it, make it private.
[color=blue]
> can a caller do this?[/color]

Almost.
[color=blue]
>
> Y y;[/color]

Isn't there an argument?
[color=blue]
> cout << y.w.size() << endl;
> y.w.pushback(6);[/color]

The member function name is 'push_back'.
[color=blue]
> cout << y.w.size() << endl;[/color]

V
  #6  
Old January 11th, 2006, 08:15 PM
W Marsh
Guest
 
Posts: n/a
Default Re: Create a Reference stl:vector attribute

On Wed, 11 Jan 2006 20:04:08 +0000, W Marsh <wayne.marsh@gmail.com>
wrote:
[color=blue]
>On 11 Jan 2006 11:48:19 -0800, ken.carlino@gmail.com wrote:
>[color=green]
>>Thanks.
>>
>>If I change it to this:
>>
>> class Y
>> {
>> public:
>> Y
>> virtual ~Y();
>>
>> vector<int> w;[/color]
>
>Why do you declare your constructors like that? It simply will not
>compile. Try:[/color]

It won't compile when you try to define a body for the constructor,
that is. Either way, it isn't doing what you think it is doing.
  #7  
Old January 11th, 2006, 08:45 PM
David Harmon
Guest
 
Posts: n/a
Default Re: Create a Reference stl:vector attribute

On 11 Jan 2006 11:48:19 -0800 in comp.lang.c++,
ken.carlino@gmail.com wrote,
[color=blue]
> vector<int> w;[/color]

Right.
[color=blue]
>Do I still need to free it in Y's destructor?[/color]

No. The compiler automatically generates the proper call to
std::vector destructor.
[color=blue]
>And since w is a public attribute, can otherside still access it?[/color]

Yes.

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles