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 167665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:18:56+00:00 2026-05-11T12:18:56+00:00

#include <iostream> using namespace std; class Foo { public: Foo(): initialised(0) { cout <<

  • 0
#include <iostream> using namespace std;  class Foo {  public:   Foo(): initialised(0)  {   cout << 'Foo() gets called AFTER test() ?!' << endl;  };   Foo test()  {   cout << 'initialised= ' << initialised << ' ?! - ';   cout << 'but I expect it to be 0 from the 'initialised(0)' initialiser on Foo()' << endl;   cout << 'this method test() is clearly working on an uninitialised object ?!' << endl;   return Foo();  }   ~Foo()  {};  private:   int initialised;  };   int main() {   //SURE this is bad coding but it compiles and runs  //I want my class to DETECT and THROW an error to prevent this type of coding  //in other words how to catch it at run time and throw 'not initialised' or something   Foo foo=foo.test();  } 
  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T12:18:57+00:00Added an answer on May 11, 2026 at 12:18 pm

    You can’t prevent people from coding poorly, really. It works just like it ‘should’:

    1. Allocate memory for Foo (which is the value of the ‘this’ pointer)
    2. Going to Foo::test by doing: Foo::test(this), in which,
    3. It gets the value by this->initialised, which is random junk, then it
    4. Calls Foo’s default constructor (because of return Foo();), then
    5. Call Foo’s copy constructor, to copy the right-handed Foo().

    Just like it should. You can’t prevent people from not knowing the right way to use C++.

    The best you could do is have a magic number:

    class A { public:     A(void) :     _magicFlag(1337)     {     }      void some_method(void)     {         assert (_magicFlag == 1337); /* make sure the constructor has been called */     }  private:     unsigned _magicFlag; } 

    This ‘works’ because the chances _magicFlag gets allocated where the value is already 1337 is low.

    But really, don’t do this.

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

Sidebar

Ask A Question

Stats

  • Questions 163k
  • Answers 163k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you are not associating short code with a URL… May 12, 2026 at 12:16 pm
  • Editorial Team
    Editorial Team added an answer I found if you apply the following styles it seems… May 12, 2026 at 12:16 pm
  • Editorial Team
    Editorial Team added an answer Use "svn switch --relocate". Look all the way at the… May 12, 2026 at 12:16 pm

Related Questions

#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
#include<iostream> using namespace std; class A { int a; int b; public: void eat()
I cannot get this simple piece of code to compile without including the TestClass.cpp
I am still learning C++, and I have never really created my own namespaces

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.