473,605 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector of const references. Is it possible?

Hi,

I wanted to create a vector of const references. Something like this:

vector<const x&y;

where x is a class name.

Is this a valid statement? Will this work if I use a reference of 'y'
somewhere else?

PJ.

Mar 7 '07 #1
13 10210
ragged_hippy wrote:
Hi,

I wanted to create a vector of const references. Something like this:

vector<const x&y;

where x is a class name.

Is this a valid statement? Will this work if I use a reference of 'y'
somewhere else?
It is not portable. const& is not assignable and therefore not a valid
container element.
Mar 7 '07 #2
On Mar 7, 12:28 pm, "ragged_hip py" <pranes...@gmai l.comwrote:
I wanted to create a vector of const references. Something like this:

vector<const x&y;

where x is a class name.

Is this a valid statement? Will this work if I use a reference of 'y'
somewhere else?
Did you try it? Any compiler will instantly tell you whether pointers
to references are allowed. (Hint: they're not, but vector requires
pointers to its containees.)

Cheers! --M

Mar 7 '07 #3

"ragged_hip py" <pr*******@gmai l.comwrote in message
news:11******** **************@ s48g2000cws.goo glegroups.com.. .
Hi,

I wanted to create a vector of const references.
Why?
Something like this:

vector<const x&y;

where x is a class name.
Not valid.
>
Is this a valid statement?
No.
Will this work if I use a reference of 'y'
No.

Standard library containers store objects. References
are not objects. References are alternate names for
existing objects.
somewhere else?
No.

You can, however, store pointers in your vector, since
pointers are objects.

Perhaps if you describe what problem you're trying to
solve, we might be able to give advice.

-Mike
Mar 7 '07 #4
On Mar 7, 11:58 am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
"ragged_hip py" <pranes...@gmai l.comwrote in message

news:11******** **************@ s48g2000cws.goo glegroups.com.. .
Hi,
I wanted to create a vector of const references.

Why?
Something like this:
vector<const x&y;
where x is a class name.

Not valid.
Is this a valid statement?

No.
Will this work if I use a reference of 'y'

No.

Standard library containers store objects. References
are not objects. References are alternate names for
existing objects.
somewhere else?

No.

You can, however, store pointers in your vector, since
pointers are objects.

Perhaps if you describe what problem you're trying to
solve, we might be able to give advice.

-Mike
Okay....I am trying to not use extra memory if possible.

I have this list of objects as a private member of a container. And I
have a public method defined in this container, that will take a
reference to vector buffer as a parameter and fill it up with objects
that it contains. If the vector for buffer is a vector of the object,
then all objects will be copied into this buffer. So, I thought may be
a vector of references might be better. But yes I immediately got his
huge set of compiler errors which I didn't expect.

Does this make sense?

-PJ.

Mar 7 '07 #5
"ragged_hip py" <pr*******@gmai l.comwrote in message
news:11******** *************@j 27g2000cwj.goog legroups.com...
On Mar 7, 11:58 am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
>"ragged_hipp y" <pranes...@gmai l.comwrote in message

news:11******* *************** @s48g2000cws.go oglegroups.com. ..
Hi,
I wanted to create a vector of const references.

Why?
Something like this:
vector<const x&y;
where x is a class name.

Not valid.
Is this a valid statement?

No.
Will this work if I use a reference of 'y'

No.

Standard library containers store objects. References
are not objects. References are alternate names for
existing objects.
somewhere else?

No.

You can, however, store pointers in your vector, since
pointers are objects.

Perhaps if you describe what problem you're trying to
solve, we might be able to give advice.

-Mike

Okay....I am trying to not use extra memory if possible.

I have this list of objects as a private member of a container. And I
have a public method defined in this container, that will take a
reference to vector buffer as a parameter and fill it up with objects
that it contains. If the vector for buffer is a vector of the object,
then all objects will be copied into this buffer. So, I thought may be
a vector of references might be better. But yes I immediately got his
huge set of compiler errors which I didn't expect.

Does this make sense?

-PJ.
So just store them as a vector of pointers.

std::vector<x*>
Mar 7 '07 #6
On Mar 7, 1:09 pm, "ragged_hip py" <pranes...@gmai l.comwrote:
[snip]
I have this list of objects as a private member of a container.
[snip]

Maybe instead of making some other container to hold this
data, you can keep holding it where it is. Maybe you can
add the functionality you want to the class that is already
doing the work of holding the data.

Alternatively, maybe the class that is doing the work of
holding this data isn't the right spot for it. Maybe it
should be held someplace else, and the work done to it
over there as well.

That is to say, possibly you have some refactoring to do.
Socks

Mar 7 '07 #7
"mlimber" <ml*****@gmail. comwrote in news:1173289964 .171996.249820@
30g2000cwc.goog legroups.com:
On Mar 7, 12:28 pm, "ragged_hip py" <pranes...@gmai l.comwrote:
>I wanted to create a vector of const references. Something like this:

vector<const x&y;

where x is a class name.

Is this a valid statement? Will this work if I use a reference of 'y'
somewhere else?

Did you try it? Any compiler will instantly tell you whether pointers
to references are allowed. (Hint: they're not, but vector requires
pointers to its containees.)
Since when? Vectors contain copies of their contained objects, not
pointers to them.
Mar 7 '07 #8
On Mar 7, 1:59 pm, Andre Kostur <nntps...@kostu r.netwrote:
"mlimber" <mlim...@gmail. comwrote in news:1173289964 .171996.249820@
30g2000cwc.goog legroups.com:
On Mar 7, 12:28 pm, "ragged_hip py" <pranes...@gmai l.comwrote:
I wanted to create a vector of const references. Something like this:
vector<const x&y;
where x is a class name.
Is this a valid statement? Will this work if I use a reference of 'y'
somewhere else?
Did you try it? Any compiler will instantly tell you whether pointers
to references are allowed. (Hint: they're not, but vector requires
pointers to its containees.)

Since when? Vectors contain copies of their contained objects, not
pointers to them.
But vectors dynamically allocate the contained objects, which in turn
requires a pointer to the value type.

Cheers! --M

Mar 7 '07 #9
"mlimber" <ml*****@gmail. comwrote in news:1173294762 .235842.253370
@j27g2000cwj.go oglegroups.com:
On Mar 7, 1:59 pm, Andre Kostur <nntps...@kostu r.netwrote:
>"mlimber" <mlim...@gmail. comwrote in news:1173289964 .171996.249820@
30g2000cwc.goo glegroups.com:
On Mar 7, 12:28 pm, "ragged_hip py" <pranes...@gmai l.comwrote:
I wanted to create a vector of const references. Something like this:
>vector<const x&y;
>where x is a class name.
>Is this a valid statement? Will this work if I use a reference of 'y'
somewhere else?
Did you try it? Any compiler will instantly tell you whether pointers
to references are allowed. (Hint: they're not, but vector requires
pointers to its containees.)

Since when? Vectors contain copies of their contained objects, not
pointers to them.

But vectors dynamically allocate the contained objects, which in turn
requires a pointer to the value type.
Probably not. As least not individually. It requires a pointer to some
memory. IIRC, TR1 specifies that it even must be contiguous memory.
Whether that actually is a pointer to type (or to array of type), or merely
a void *, that's an implementation detail. What's probably happening
behind the scenes is that the vector allocates "sizeof(typ e) * capacity"
bytes of memory, and uses placement new in each position that holds a valid
object.

From a vector user's point of view, all that is required is that the type
contained in the vector is copy constructable and assignable (and that the
constructed/assigned to object is the same as the original, which is why
auto_ptr can't be stored in STL containers). There are no pointers
anywhere in sight.
Mar 8 '07 #10

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

Similar topics

4
1750
by: hrmadhu | last post by:
Hi, I wish to declare a vector of deque of int, which I do as follows. #include<vector> #include<deque> #include<iostream> using namespace std; int main(int argc, char* argv) {
2
1807
by: The Directive | last post by:
I'm confused as to why C++ does not allow to create a vector of references (even though a vector of pointers can be created). I read some old threads about it but I still don't fully understand it. Can someone help? What about array of references, how would I declare it?
1
3374
by: red floyd | last post by:
Both gcc 3.3.1 and MSVC 7.1 complain about the second for_each() call in the following code. Why can't I use bind2nd for a functor with a non-const reference as the second parameter? I have to use kludge it by using something like KludgeFunctor(). --------------------------- #include <vector> #include <algorithm> #include <functional>
7
1379
by: raj | last post by:
I need explanation of the behavior of the following code. When run as it is, it gives error. //-------------------------<start>------------------ #include <iostream.h> #include <stdlib.h> class ABC { private: int x;
2
1674
by: matthias_k | last post by:
Hello, I'm wondering if returning const references to a class member is generally faster than returning a copy of the object. Consider this code: class A { std::string data; public:
3
1338
by: andreykuzmenko | last post by:
I am trying to create a template class, which would store a vector of references to objects of the same class. Like: template<class _core_t> class Neuron { ///snipped private: vector<Neuron<_core_t>*> _neighbors;
10
1527
by: ATASLO | last post by:
In the following example, section #3 fails under VC98, VC2003, VC2005 Express Beta (Aug 2004) and g++ 3.3.2. Is this just a pitfall of the C++ specification? Why don't any of the above compilers at least flag this as a warning as they would when say trying to return a const & to a local? In Section #2, the const B& Bref is initialized and bound to the temporary returned from GetSettings(). That is the temporary B exists until Bref goes...
2
1655
by: vineoff | last post by:
When you should not or can't use const references? Like void f ( const Foo& b) ; or int main() { const int& r = 5; }
0
1274
by: Chris Thomasson | last post by:
I am fine-tuning some last minute things in my c++ allocator lib and was thinking of ways to manage local references to system objects created with factory functions. I started to tinker around with using const references to represent a smart-pointer template that uses intrusive reference counting. Anyway, here is a short little program that should clearly demonstrate what I am messing around with: _________________________ #include...
0
8001
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, 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...
0
7934
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,...
0
8415
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 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...
1
8069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8286
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6742
agi2029
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...
1
5886
isladogs
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...
1
1537
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1270
bsmnconsultancy
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...

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.