Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8657755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:36:01+00:00 2026-06-12T15:36:01+00:00

Possible Duplicate: std::string x(x); class A {}; int main() { A a(a); } This

  • 0

Possible Duplicate:
std::string x(x);

class A {};

int main() {

    A a(a);
}

This compiles.

gcc (GCC) 4.7.2 20120921 (Red Hat 4.7.2-2)
g++ -o main main.cpp -Wall -w -ansi

I receive no warnings.

Why does this appear to be valid C++?
Is this mentioned anywhere in the standard?
Are there warning flags that can report this for gcc?

When the class has member data, the data ends up random.
example:

#include <iostream>

class A {

public:
    int i;
    A() : i{6} {}
};

int main() {

    A a(a);
    std::cout << a.i << '\n';
}

output: -482728464

What’s going on here?
Also, how can I prevent myself from accidently doing this? – Is it possible to make it a compiler error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:36:02+00:00Added an answer on June 12, 2026 at 3:36 pm

    (§ 3.3.2/1) The point of declaration for a name is immediately after its complete declarator (Clause 8) and before its initializer (if any), except as noted below. [ Example:

    int x = 12;
    { int x = x; }
    

    Here the second x is initialized with its own (indeterminate) value. —end example ]

    This applies to user-defined types, such as your class A, as well. The copy constructor used is the default one, auto-generated by the compiler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Remove spaces from std::string in C++ I am starting to learn cpp.
Possible Duplicate: Cost of using std::map with std::string keys vs int keys? if I
Possible Duplicate: Append an int to a std::string I want to add string and
Possible Duplicate: C++ std::string conversion problem on Windows How to convert std::string to LPCSTR?
Possible Duplicate: How is std::iostream buffered? This might sound ridiculous, but how can I
Possible Duplicate: Copy Constructor is not invoked # include <iostream> using namespace std; class
Possible Duplicate: Calling class method through NULL class pointer I was asked this question
Possible Duplicate: Maximum length of a std::basic_string<_CharT> string I would like to know how
Possible Duplicate: std::string and its automatic memory resizing I am just curious, how are
Possible Duplicate: Printing a string to a temporary stream object in C++ std::ostringstream printing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.