>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>(ali st.size()) )
{
}
And I get this compilation error:
.../src/YMapData.cpp:9: error: invalid initialization of non-const
reference of type 'std::vector<in t, std::allocator< int> >&' from a
temporary of type 'std::vector<in t, std::allocator< int> >'
I appreciate if someone can tell me what did I do wrong. 6 2145 ke*********@gma il.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>(ali st.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<in t, std::allocator< int> >&' from a temporary of type 'std::vector<in t, std::allocator< int> >'
I appreciate if someone can tell me what did I do wrong.
You will need to learn to _instantiate_ objects.
V
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;
On 11 Jan 2006 11:48:19 -0800, ke*********@gma il.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. ke*********@gma il.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
On Wed, 11 Jan 2006 20:04:08 +0000, W Marsh <wa*********@gm ail.com>
wrote: On 11 Jan 2006 11:48:19 -0800, ke*********@gma il.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.
On 11 Jan 2006 11:48:19 -0800 in comp.lang.c++, ke*********@gma il.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 up with v1=5,4,3,2,1 or perhaps leave v1 intact and
generate a new vector v2=5,4,3,2,1
so, does the STL have something allready? or do I have to do it manualy?
Thanks
|
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 generateVals()
{
.....
//generate some values
|
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.
|
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?
|
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;
| |
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>
|
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 simply
an array index integer referring to a position in the array.
The reason I wonder this is because firstly, the implementation will
need to convert the iterator to an array index integer anyway, because
the iterator may become invalidated...
|
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 simultaneously for reading and
writing purpose. Sometimes, application getting crash. I think, vectors are
threadsafe. It is getting crash at vector iterators. I checked in call stack
also. How to avoid this problem without synchronization?
--
|
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_ vector<POINTvertices) PURE;
};
Is it possible to import STL containers to C# at all?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |