473,883 Members | 3,128 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Alias for a std::vector

I am trying to refer to the same std::vector in a class by two different names,
I tried a union, and I tried a reference, I can't seem to get the syntax right.
Can anyone please help? Thanks
Aug 16 '06
56 5851
Gavin Deane <de*********@ho tmail.comwrote:
It is perfectly possible to assign to a (non-const) reference and
thereby reseat it, including reference class members.
How do you reseat a reference? I thought this was always impossible in
a conformant program.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Aug 17 '06 #31
Gavin Deane wrote:
Thomas J. Gritzan wrote:
>So references and const member variables have to use initialization
list, since assignment is not possible.

It is perfectly possible to assign to a (non-const) reference and
thereby reseat it, [..]
Are you sure? You seem a bit confused about what "reseating" means
and what the effects of assigning to a reference would be.

#include <cassert>

struct A {
int a;
};

struct B {
A &ra;
B(A& r) : ra(r) {}
};

int main() {
A a1 = {1}, a2 = {2};
B ba1(a1), ba2(a2), ba1_again(a1);

// program runs fine now.
// please _reseat_ ba1.ra to refer to a2
// without any reconstruction of ba1.
// and see if the program still works

assert(ba1_agai n.ra.a == 1);
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 17 '06 #32
Marcus Kwok wrote:
Gavin Deane <de*********@ho tmail.comwrote:
>It is perfectly possible to assign to a (non-const) reference and
thereby reseat it, including reference class members.

How do you reseat a reference? I thought this was always impossible
in a conformant program.
One can play a reconstruction trick using placement new. The same trick
is played when "chaining" constructors. I don't use it, and many people
consider those tricks rather unnecessary. There are always right ways to
use the tool and wrong ways.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 17 '06 #33

Victor Bazarov wrote:
Gavin Deane wrote:
Thomas J. Gritzan wrote:
So references and const member variables have to use initialization
list, since assignment is not possible.
It is perfectly possible to assign to a (non-const) reference and
thereby reseat it, [..]

Are you sure?
No
You seem a bit confused
I was. I'm better now.
about what "reseating" means
and what the effects of assigning to a reference would be.
Thanks for catching that.

Gavin Deane

Aug 17 '06 #34

"Gavin Deane" <de*********@ho tmail.comwrote in message
news:11******** **************@ p79g2000cwp.goo glegroups.com.. .
>
Peter Olcott wrote:
>"Gavin Deane" <de*********@ho tmail.comwrote in message
You are aware that initialisation and assignment are two entirely
different things aren't you? It seems odd to object that two entirely
different concepts have entirely different syntax.

Initializati on is merely the first assignment.

If you read that in a book, throw the book away. If you read it on a
website, delete the website from your list of favourites. Search the
history of this group for countless discussions of the difference
between initialisation and assignment.

You've already discovered that with a class member of reference type,
by the time you first get the chance to assign to it, the opportunity
to initialise it (and references must be initialised) has been missed
and so the code cannot be correct.

Gavin Deane
Initialization, semantically across languages is merely the first assignment.
The fact that C++ has things that can only be initialized and not otherwise
assigned to does not change this fundamental essential semantic meaning.
Initialization is still simply a special case of assignment.
Aug 17 '06 #35
Peter Olcott wrote:
"Gavin Deane" <de*********@ho tmail.comwrote in message
news:11******** **************@ p79g2000cwp.goo glegroups.com.. .
>>
Peter Olcott wrote:
>>"Gavin Deane" <de*********@ho tmail.comwrote in message
You are aware that initialisation and assignment are two entirely
different things aren't you? It seems odd to object that two
entirely different concepts have entirely different syntax.

Initializatio n is merely the first assignment.

If you read that in a book, throw the book away. If you read it on a
website, delete the website from your list of favourites. Search the
history of this group for countless discussions of the difference
between initialisation and assignment.

You've already discovered that with a class member of reference type,
by the time you first get the chance to assign to it, the opportunity
to initialise it (and references must be initialised) has been missed
and so the code cannot be correct.

Gavin Deane

Initialization, semantically across languages is merely the first
assignment. The fact that C++ has things that can only be initialized
and not otherwise assigned to does not change this fundamental
essential semantic meaning. Initialization is still simply a special
case of assignment.
C++ has things that cannot be assigned to (arrays) and things that when
assigned to just create an illusion of that (references). Both, however,
have clear initialisation meaning and requirements. Why do you need to
mix the two distinct operations, is beyond my comprehension. C++ is not
"other languages" in many aspects, and besides, we don't discuss "other
languages" here.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 17 '06 #36

"Victor Bazarov" <v.********@com Acast.netwrote in message
news:ec******** **@news.datemas .de...
Peter Olcott wrote:
>"Gavin Deane" <de*********@ho tmail.comwrote in message
news:11******* *************** @p79g2000cwp.go oglegroups.com. ..
>>>
Peter Olcott wrote:
"Gavin Deane" <de*********@ho tmail.comwrote in message
You are aware that initialisation and assignment are two entirely
different things aren't you? It seems odd to object that two
entirely different concepts have entirely different syntax.

Initializati on is merely the first assignment.

If you read that in a book, throw the book away. If you read it on a
website, delete the website from your list of favourites. Search the
history of this group for countless discussions of the difference
between initialisation and assignment.

You've already discovered that with a class member of reference type,
by the time you first get the chance to assign to it, the opportunity
to initialise it (and references must be initialised) has been missed
and so the code cannot be correct.

Gavin Deane

Initialization , semantically across languages is merely the first
assignment. The fact that C++ has things that can only be initialized
and not otherwise assigned to does not change this fundamental
essential semantic meaning. Initialization is still simply a special
case of assignment.

C++ has things that cannot be assigned to (arrays) and things that when
assigned to just create an illusion of that (references). Both, however,
have clear initialisation meaning and requirements. Why do you need to
mix the two distinct operations, is beyond my comprehension. C++ is not
"other languages" in many aspects, and besides, we don't discuss "other
languages" here.
Instead of C++ overloading the meaning of the term [initialization] to make it
mean something slightly different than what it means everywhere else, C++ should
have adopted the universal convention of the meaning of this term. If they want
to have something slightly different than what [initialization] means everywhere
else, they could come up with a different term. In any case there is no absolute
requirement for initialization lists, the same semantics could be derived using
the assignment operator. In the case of things such as references, this
initialization form of assignment would only be valid in declarations.
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Aug 17 '06 #37

Peter Olcott skrev:
Instead of C++ overloading the meaning of the term [initialization] to make it
mean something slightly different than what it means everywhere else, C++ should
have adopted the universal convention of the meaning of this term. If they want
to have something slightly different than what [initialization] means everywhere
else, they could come up with a different term. In any case there is no absolute
requirement for initialization lists, the same semantics could be derived using
the assignment operator. In the case of things such as references, this
initialization form of assignment would only be valid in declarations.
You simply don't get it. The difference between C++ and most other
languages is that most other languages don't allow you to have special
initialisation: they have only assignment. This is the case in e.g. C
or Pascal (where there is no initialisation) and Java (where the
initialisation is system-defined). So those other languages do not need
to have a special notion of initialisation precisely because there us
none. This is related to the rules in C++ that guarantee destruction
and that objects always are in a valid state. It is for exactly the
same reason that initialisation-lists are a necessary, clean and
unavoidable part of the syntax.

/Peter

Aug 17 '06 #38

"peter koch" <pe************ ***@gmail.comwr ote in message
news:11******** **************@ 75g2000cwc.goog legroups.com...
>
Peter Olcott skrev:
>Instead of C++ overloading the meaning of the term [initialization] to make
it
mean something slightly different than what it means everywhere else, C++
should
have adopted the universal convention of the meaning of this term. If they
want
to have something slightly different than what [initialization] means
everywhere
else, they could come up with a different term. In any case there is no
absolute
requirement for initialization lists, the same semantics could be derived
using
the assignment operator. In the case of things such as references, this
initializati on form of assignment would only be valid in declarations.
You simply don't get it. The difference between C++ and most other
languages is that most other languages don't allow you to have special
initialisation: they have only assignment. This is the case in e.g. C
or Pascal (where there is no initialisation) and Java (where the
initialisation is system-defined). So those other languages do not need
to have a special notion of initialisation precisely because there us
none. This is related to the rules in C++ that guarantee destruction
and that objects always are in a valid state. It is for exactly the
same reason that initialisation-lists are a necessary, clean and
unavoidable part of the syntax.

/Peter
Why couldn't the same thing be accomplished using the operator=() syntax?
Aug 17 '06 #39
Peter Olcott wrote:
"peter koch" <pe************ ***@gmail.comwr ote in message
news:11******** **************@ 75g2000cwc.goog legroups.com...
>>
Peter Olcott skrev:
>>Instead of C++ overloading the meaning of the term [initialization]
to make it
mean something slightly different than what it means everywhere
else, C++ should
have adopted the universal convention of the meaning of this term.
If they want
to have something slightly different than what [initialization]
means everywhere
else, they could come up with a different term. In any case there
is no absolute
requirement for initialization lists, the same semantics could be
derived using
the assignment operator. In the case of things such as references,
this initialization form of assignment would only be valid in
declaration s.
You simply don't get it. The difference between C++ and most other
languages is that most other languages don't allow you to have
special initialisation: they have only assignment. This is the case
in e.g. C or Pascal (where there is no initialisation) and Java
(where the initialisation is system-defined). So those other
languages do not need to have a special notion of initialisation
precisely because there us none. This is related to the rules in C++
that guarantee destruction and that objects always are in a valid
state. It is for exactly the same reason that initialisation-lists
are a necessary, clean and unavoidable part of the syntax.

/Peter

Why couldn't the same thing be accomplished using the operator=()
syntax?
Same thing as in

Widget myChildWidget(0 ,0,width,height ,myMainWidget);

? How would you do it? This has to invoke a constructor. Once it's
invoked, you have to initialise the members

class Widget {
Canvas myCanvas;
Widget & parent;
public:
Widget(int x, int y, int w, int h, Widget& parent)
: myCanvas(x, y, w, h), myParent(parent ) { }

Where and how would you use your "operator=( ) syntax"?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 17 '06 #40

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

Similar topics

20
17876
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
32
69731
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: std::vector<int> fun(){ //build the vector v; return v; }
21
2218
by: Peter Olcott | last post by:
I got the previous alias to std::vector working, and found that it takes up the space of a pointer. I want to find a way to do an alias to a std::vector that does not take up any space. Is there any way to do this? (I tried #define Name1 Name2 and it didn't compile)
0
9944
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
11154
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...
1
10863
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
10422
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...
1
7977
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
6005
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4622
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
2
4228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3241
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.