Create a Reference stl:vector attribute

K

ken.carlino

From http://www.parashift.com/c++-faq-lite/references.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.
 
V

Victor Bazarov

said "Use references when you can, and pointers when you have to."

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

Should simply be

vector said:
};

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

Should instead be

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

You will need to learn to _instantiate_ objects.

V
 
K

ken.carlino

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;
 
V

Victor Bazarov

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?

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

It's not, so it will destroyed without your attention.
And since w is a public attribute, can otherside still access it?

Yes. If you're concerned with it, make it private.
can a caller do this?
Almost.


Y y;

Isn't there an argument?
cout << y.w.size() << endl;
y.w.pushback(6);

The member function name is 'push_back'.
cout << y.w.size() << endl;

V
 
W

W Marsh

Thanks.

If I change it to this:

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

vector<int> w;

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

class Y
{
public:
Y();
virtual ~Y();
/* ... */
};
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?

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

Yes, but having public data in classes isn't particularly good OOP
design.
 
W

W Marsh

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

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

David Harmon

On 11 Jan 2006 11:48:19 -0800 in comp.lang.c++,
(e-mail address removed) wrote,
vector<int> w;
Right.

Do I still need to free it in Y's destructor?

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

Yes.
 

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

Forum statistics

Threads
474,142
Messages
2,570,820
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top