Hi,
Consider the following code snipet:
void f(const std::string & msg)
{
//...
for (...)
{
//...
std::string msg = msg + " " + another_string; //1
//...
}
//...
}
This code is the consequence of lazy Search/Replace (s/msg0/msg/g -
initially the function parameter and the first variable after the _=_
was msg0).
But shouldn't the compiler complain about the initialization of an
object use the same object? What am I missing?
TIA,
Marcelo Pinto 15 2051
Marcelo Pinto wrote: Hi,
Consider the following code snipet:
void f(const std::string & msg) { //... for (...) { //... std::string msg = msg + " " + another_string; //1 //... } //... }
But shouldn't the compiler complain about the initialization of an object use the same object? What am I missing?
Which compiler? The above code (after removing all extra portion) gives
redeclaration error with Comeau online (msg has already been declared
in the current scope)
Marcelo Pinto wrote: Hi,
Consider the following code snipet:
void f(const std::string & msg) { //... for (...) { //... std::string msg = msg + " " + another_string; //1 //... } //... }
This code is the consequence of lazy Search/Replace (s/msg0/msg/g - initially the function parameter and the first variable after the _=_ was msg0). But shouldn't the compiler complain about the initialization of an object use the same object? What am I missing?
you are allowed to redeclare already visible identifiers in different
scopes. only redefining an identifier that has already been defined in
the same scope is illegal and yields a compiler error.
your for() loop body is a new scope, thus the identifier msg can be
defined again. you have to be aware though that you are effectively
hiding the already visible identifier, which is probably not what you
intended to do.
-- peter
Neelesh Bodas wrote: Marcelo Pinto wrote:
Hi,
Consider the following code snipet:
void f(const std::string & msg) { //... for (...) { //... std::string msg = msg + " " + another_string; //1 //... } //... }
But shouldn't the compiler complain about the initialization of an
object use the same object? What am I missing?
Which compiler? The above code (after removing all extra portion) gives redeclaration error with Comeau online (msg has already been declared in the current scope)
Really? Did you remove the 'for' as well? It's introduces the proper
scope limitation for the "inner" msg...
V
Neelesh Bodas wrote: Marcelo Pinto wrote: Hi,
Consider the following code snipet:
void f(const std::string & msg) { //... for (...) { //... std::string msg = msg + " " + another_string; //1 //... } //... }
But shouldn't the compiler complain about the initialization of an object use the same object? What am I missing?
Which compiler? The above code (after removing all extra portion) gives redeclaration error with Comeau online (msg has already been declared in the current scope)
you probably removed a little bit too much of the given example... ;-)
-- peter
Marcelo Pinto wrote: void f(const std::string & msg) {
{ std::string msg = msg + " " + another_string; //1
} }
.... But shouldn't the compiler complain about the initialization of an object use the same object? What am I missing?
The Standard clearly states, that in
int x = 12;
{ int x = x; }
the second _x_ is initialized with its own (indeterminate) value.
But in this particular case I wonder what will happen, since _msg_
should probably be constructed before it is used. My belief is that
_msg_ will be constructed with the default constructor, then the
expression will be evaluated and then the assignment operator will be
called.
Martin.
"Marcelo Pinto" <mp******@gmail .com> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.com... Hi,
Consider the following code snipet:
void f(const std::string & msg) { //... for (...) { //... std::string msg = msg + " " + another_string; //1
This is legal. This msg local to the for loop hides the msg parameter. So
it basically becomes:
std::string msg = "" + " " + another_string;
which is probably why you're asking.
No, there are generally no compiler warnings for hiding variables. Although
there generally are for hiding methods in classes.
//... } //... }
This code is the consequence of lazy Search/Replace (s/msg0/msg/g - initially the function parameter and the first variable after the _=_ was msg0). But shouldn't the compiler complain about the initialization of an object use the same object? What am I missing?
TIA,
Marcelo Pinto
Neelesh Bodas escreveu: Which compiler? The above code (after removing all extra portion) gives redeclaration error with Comeau online (msg has already been declared in the current scope)
aCC: HP ANSI C++ B3910B A.03.37
Neelesh Bodas wrote: Marcelo Pinto wrote:void f(const std::string & msg) { //... for (...) { //... std::string msg = msg + " " + another_string; //1 //... } //... } Which compiler? The above code (after removing all extra portion) gives redeclaration error with Comeau online (msg has already been declared in the current scope)
It can hardly be redeclared as long as it is in the inner scope. You
probably removed the "extra" for loop. Don't do that or at least remove
only `for (...)` line.
Martin.
Jim Langston wrote: "Marcelo Pinto" <mp******@gmail .com> wrote in message news:11******** *************@g 44g2000cwa.goog legroups.com... Hi,
Consider the following code snipet:
void f(const std::string & msg) { //... for (...) { //... std::string msg = msg + " " + another_string; //1
This is legal. This msg local to the for loop hides the msg parameter. So it basically becomes: std::string msg = "" + " " + another_string;
which is probably why you're asking.
No, there are generally no compiler warnings for hiding variables. Although there generally are for hiding methods in classes.
The local "msg" variable is accessed before it has been initialized
(because it is used in its own initializer.) Since the value of the msg
variable is indeterminate before its initialization, the outome of this
expression, though legal, is undefined.
Greg This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Vinay Aggarwal |
last post by:
I have been thinking about the lazy initialization and double checked
locking problem. This problem is explain in detail here
http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
I am not fully convinced that this problem cannot be
solved. I am going to propose a solution here. For the sake of
discussion I will post my solution here. It is possible that the
proposed solution does not work, feedback and comments are...
|
by: Russell Silva |
last post by:
I have a class A with a member variable type vector<foo> which is
initialized upon construction (no standard () constructor):
class A {
protected:
vector<foo> foo_vector;
public:
A(const vector<foo>& init_foo_vector);
}
|
by: DanielBradley |
last post by:
Hello all,
I have recently been porting code from Linux to cygwin and came across
a problem with static const class members (discussed below). I am
seeking to determine whether I am programming non-standard C++ or if the
problem lies elsewhere.
To summarize static const class members are not being accessed properly
when accessed from a DLL by another external object file (not within the
DLL). It only occurs when the static const...
|
by: JKop |
last post by:
What's the difference between them?
Take the following:
#include <iostream>
struct Blah
{
int k;
|
by: Brian Folke Seaberg |
last post by:
I have been examining the documentation for a class library written in C++.
It seems that most of the classes in that library use two stage initialization.
Objects are not ready for use after construction. Objects are ready for use
only after post-constructor initialization functions have been executed.
I prefer one stage initialization. I believe that upon completion of object
creation an object should be in a valid state and...
| |
by: Anton Pervukhin |
last post by:
Hi everybody!
I have a small problem regarding the initialization of pointer to the
file stream under some conditions.
Imagine a class which has a pointer to output file stream and some
additinional methods to deal with it, i.e. open/close/write:
classA {
private:
const std::auto_ptr<std::ofstream> filePtr;
|
by: Luke Meyers |
last post by:
Lately I find myself increasingly preferring the practice of going
ever-so-slightly out of my way to avoid the use of the form of
initialization which uses the '=' symbol, on the grounds that it can be
mistaken too easily for assignment. I like the avoidance of mistakes.
I like even more the frequent, subtle reinforcement (for myself and my
colleagues) of the point that assignment and initialization are
entirely different operations.
...
|
by: Jesper Schmidt |
last post by:
When does CLR performs initialization of static variables in a class
library?
(1) when the class library is loaded
(2) when a static variable is first referenced
(3) when...
It seems that (1) holds for unmanaged C++ code, but not for managed
code. I have class library with both managed and unmanaged static
variables that are not referenced by any part of the program. All the
|
by: Steve Folly |
last post by:
Hi,
I had a problem in my code recently which turned out to be the 'the "static
initialization order fiasco"' problem
(<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>)
The FAQ section describes a solution using methods returning references to
static objects.
But consider:
|
by: subramanian100in |
last post by:
Suppose we have a class named Test.
Test obj; // assuming default ctor is available
Test direct_init(obj); // direct initialization happens here
Test copy_init = obj; // copy initialization happens here
Suppose we have a function
void fn(Test arg);
|
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: 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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |