473,388 Members | 1,342 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,388 software developers and data experts.

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.

Jan 11 '06 #1
6 2136
ke*********@gmail.com wrote:
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."


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<int> w;
};

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
Jan 11 '06 #2
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;

Jan 11 '06 #3
On 11 Jan 2006 11:48:19 -0800, ke*********@gmail.com wrote:
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.
Jan 11 '06 #4
ke*********@gmail.com wrote:
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
Jan 11 '06 #5
On Wed, 11 Jan 2006 20:04:08 +0000, W Marsh <wa*********@gmail.com>
wrote:
On 11 Jan 2006 11:48:19 -0800, ke*********@gmail.com wrote:
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:


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.
Jan 11 '06 #6
On 11 Jan 2006 11:48:19 -0800 in comp.lang.c++,
ke*********@gmail.com 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.

Jan 11 '06 #7

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

Similar topics

6
by: Vasileios Zografos | last post by:
Hi there I am using the STL vector and I was wondering, is there an existing method for reversing the contents of one vector? e.g. vector<int> v1 which has entries 1,2,3,4,5 and I want to end...
6
by: Vasileios Zografos | last post by:
Hello, I have a function that generates some values (e.g. vertices in 2d space) the number of which I dont know. So, it could generate 20 vertices, 100 vertices, or even 1 vertex. void...
7
by: silverburgh.meryl | last post by:
in STL, why vector has an API to return the n-th element, but list does not have such API? http://www.sgi.com/tech/stl/Vector.html http://www.sgi.com/tech/stl/List.html Thank you.
2
by: Sue | last post by:
I need to use STL vectors. Especially, I need limited number of vectors(more than 10) Is it possible to use array of STL vectors? How to make it?
9
by: Christian Chrismann | last post by:
Hi, I've a runtime problem with STL vectors. Here is the simplified version of the code: template <class Tclass A { ... private: vector<T*myvector; typename vector<T*>::itarator mIt;
1
by: krunalbauskar | last post by:
Hi, Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. For example - <snippet> #include <iostream> #include <vector>
3
by: chsalvia | last post by:
I have a question about the design of STL vector. One thing I wonder was why the STL designers chose to have the insert() and erase() functions take an iterator as the first argument, rather than...
1
by: =?Utf-8?B?QWxleA==?= | last post by:
Hi all, I am devloping an application in VC++ with Visual Studio 2005. In this applicatin, Threadpool and stl classes are there. All threads will use(sending by pointer to vector) stl vector...
2
by: Builder | last post by:
How can I import the following COM interface to C#? DECLARE_INTERFACE_(IVertices, IUnknown) { STDMETHOD(get_vertices) (THIS_ vector<POINT>& vertices) PURE; STDMETHOD(set_vertices) (THIS_...
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:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.