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

  • Home
  • SEARCH
  • 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 985729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:12:31+00:00 2026-05-16T05:12:31+00:00

Using C++ I built a Class that has many setter functions, as well as

  • 0

Using C++ I built a Class that has many setter functions, as well as various functions that may be called in a row during runtime.
So I end up with code that looks like:

A* a = new A();
a->setA();
a->setB();
a->setC();
...
a->doA();
a->doB();

Not, that this is bad, but I don’t like typing “a->” over and over again.
So I rewrote my class definitions to look like:

class A{
public:
    A();
    virtual ~A();

    A* setA();
    A* setB();
    A* setC();
    A* doA();
    A* doB();

    // other functions

private:

    // vars
};

So then I could init my class like: (method 1)

A* a = new A();
a->setA()->setB()->setC();
...
a->doA()->doB();

(which I prefer as it is easier to write)
To give a more precise implementation of this you can see my SDL Sprite C++ Class I wrote at http://ken-soft.com/?p=234

Everything seems to work just fine. However, I would be interested in any feedback to this approach.
I have noticed One problem. If i init My class like: (method 2)

A a = A();
a.setA()->setB()->setC();
...
a.doA()->doB();

Then I have various memory issues and sometimes things don’t work as they should (You can see this by changing how i init all Sprite objects in main.cpp of my Sprite Demo).
Is that normal? Or should the behavior be the same?
Edit the setters are primarily to make my life easier in initialization. My main question is way method 1 and method 2 behave different for me?

Edit: Here’s an example getter and setter:

Sprite* Sprite::setSpeed(int i) {
    speed = i;
    return this;
}

int Sprite::getSpeed() {
    return speed;
}
  • 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-05-16T05:12:31+00:00Added an answer on May 16, 2026 at 5:12 am

    One note unrelated to your question, the statement A a = A(); probably isn’t doing what you expect. In C++, objects aren’t reference types that default to null, so this statement is almost never correct. You probably want just A a;

    A a creates a new instance of A, but the = A() part invokes A‘s copy constructor with a temporary default constructed A. If you had done just A a; it would have just created a new instance of A using the default constructor.

    If you don’t explicitly implement your own copy constructor for a class, the compiler will create one for you. The compiler created copy constructor will just make a carbon copy of the other object’s data; this means that if you have any pointers, it won’t copy the data pointed to.

    So, essentially, that line is creating a new instance of A, then constructing another temporary instance of A with the default constructor, then copying the temporary A to the new A, then destructing the temporary A. If the temporary A is acquiring resources in it’s constructor and de-allocating them in it’s destructor, you could run into issues where your object is trying to use data that has already been deallocated, which is undefined behavior.

    Take this code for example:

    struct A {
        A() { 
            myData = new int;
            std::cout << "Allocated int at " << myData << std::endl;
        }
        ~A() { 
            delete myData; 
            std::cout << "Deallocated int at " << myData << std::endl;
        }
        int* myData;
    };
    
    A a = A();
    cout << "a.myData points to " << a.myData << std::endl;
    

    The output will look something like:

    Allocated int at 0x9FB7128
    Deallocated int at 0x9FB7128
    a.myData points to 0x9FB7128
    

    As you can see, a.myData is pointing to an address that has already been deallocated. If you attempt to use the data it points to, you could be accessing completely invalid data, or even the data of some other object that took it’s place in memory. And then once your a goes out of scope, it will attempt to delete the data a second time, which will cause more problems.

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

Sidebar

Related Questions

I'm using a Java class library that is in many ways incomplete: there are
when scanning a file for words and using the built-in hashset class from the
I want to begin using DBForge and migrations class thats built into CI but
I'm using the PHPMailer-Lite class to build an email sending script and I'm not
I have build a c# class library verification.dll using OpenCVSharp. This references OpenCvSharp.dll in
I am trying to build a cache class using the Ardalis Example http://ardalis.com/introducing-the-cachedrepository-pattern I
I'm using Codeigniter and the Active Record class to build a simple API in
I'm trying to build the proxy class for a web service using the wsdl
I could build a sip profile using SipProfile.Builder class. I used following snippet of
I'm using flask-sqlalchemy to build a webapp and I have the following model(s): class

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.