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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:51:06+00:00 2026-06-15T01:51:06+00:00

I have the following class in c++ class MyClass { public: bool flag; MyClass

  • 0

I have the following class in c++

class MyClass {

public:
  bool flag;

  MyClass (bool flag = false) {
    this->flag = flag;
  }
...
};

In my main (used for testing) I have something like so

int main() {
 MyClass number1 = MyClass(true);
 MyClass number2 = MyClass(false);
 MyClass number3 = new MyClass(false);
 MyClass number4 = new MyClass(true);
 MyClass number5 = new MyClass();
 std::cout << number1.flag << number2.flag << std::endl;
 std::cout << number3.flag << number4. flag << number5.flag << std::endl;
}

And my output (imagine boolean outputs true or false and the output is formatted better)

true, false 
true, true, true

The problem is when I use the “new” keyword to make a “MyClass” object it will always set the flag variable to true regardless of what I do or don’t put into the argument constructor.

It works for the constructors not using “new” but not the ones using it. The only real difference I know between using and not using the keyword new is that if you don’t use it, the variable is created on the stack, otherwise it is created on the heap. I don’t see any way this could keep coming up wrong.

What would need to change if I wanted it to work properly?
I do have other methods in the class that can successfully modify the flag variable, but initializing it doesn’t work. I also tried using the member initialization list to construct the variable but that doesn’t work either.

Thanks for your help!

Edit: Here is the code that does compile. I have two files, Test.c++ and AbstractCell.h. AbstractCell is MyClass and when this code runs it outputs “isAlive: 1”

 void test_abstract_2(){
   AbstractCell one = new AbstractCell();

      std::cout << std::endl <<  "isAlive:  " << one.alive << std::endl;

  }
  • 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-15T01:51:07+00:00Added an answer on June 15, 2026 at 1:51 am

    The pointer returned by new gets implicitly converted to bool, which is then passed to MyClass(bool).

    In other words, the following:

    MyClass number3 = new MyClass(false);
    

    is equivalent to:

    MyClass number3(bool(new MyClass(false)));
    

    Since the pointer returned by new is never null, the conversion always yields true, which is what gets passed to number3‘s constructor. The newly allocated pointer gets leaked.

    You probably meant to say:

     MyClass* number3 = new MyClass(false);
     MyClass* number4 = new MyClass(true);
     MyClass* number5 = new MyClass();
    

    (note the asterisks.)

    You would not have had this problem if you declared the constructor explicit:

      explicit MyClass (bool flag = false) {
        this->flag = flag;
      }
    

    The code would fail to compile, which would immediate tell you that something was amiss.

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

Sidebar

Related Questions

I have the following pattern: template <int a, int b> class MyClass { public:
I have a class that looks like the following: public class MyClass { private
Let's say I have the following class: class MyClass { private: int Data; public:
Let say I have a class like the following: class MyClass { public function
I have the following method: class MyClass { public: MyClass; bool method (MyClass &obj);
If I have the following code: public class MyClass { public string myName {
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
Say I have the following: Class myclass { public string stra =, strb =
I have the following class: public static class MyClass { private static readonly Dictionary<Type,
Suppose I have the following class: template <typename T> class MyClass { public: void

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.