473,989 Members | 18,077 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 10347
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
1769
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
1823
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
3395
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
1403
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
1691
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
1356
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
1563
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
1672
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
1297
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
10402
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
11914
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, 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...
0
11488
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
11704
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,...
1
8542
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...
0
7691
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();...
0
6494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6657
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5241
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.