Can the same object initialize a private variable via a constructor more than once

It'd be convenient to just update a private member variable whenever I
wanted without having to declare a new object every time. Is this possible?
Thanks for any response.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
///Playing with inheritance and other stuff
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;

class yeehaw
{
    private:
            int ketchup;

    protected:
            string kong;///* try to access this directly from child class *
            
    public:
            yeehaw()
            {
                ketchup = 0;
            }

            yeehaw(int a) : ketchup(a) {}   ///New notation

            void printKetchup() { cout<<ketchup<<endl; }
};

int main()
{
    yeehaw yoshi;

    cout<<"Before initializing ketchup to 25: ";
    yoshi.printKetchup();

    ///will not let me initialize constructor to a new value w/out declaring new object
    yeehaw mcdouble(25);

    cout<<"After initializing ketchup to 25: ";
    mcdouble.printKetchup();

    return 0;
}
Just add a setter...
Another way is to write an assignment operator. For example

1
2
3
4
5
yeehaw & operator =( int red )
{
   ketchup = red;
   return ( *this );
}


Thanks. That's what I would do but just wanted to know if it was even possible. I've never heard of operator...will check it out.thanks again
Last edited on
I just tried to play with but I don't get it Vlad. Is the yeehaw function you specified a constructor. How would I call it?..looking up operator now...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class yeehaw
{
    private:
            int ketchup;

    protected:
            string kong;///* try to access this directly from child class *
            
    public:
            yeehaw()
            {
                ketchup = 0;
            }

            yeehaw(int a) : ketchup(a) {}   ///New notation
            yeehaw & operator =( int red )
            {
                ketchup = red;
                return ( *this );
            }
            void printKetchup() { cout<<ketchup<<endl; }
};


int main()
{
    yeehaw yoshi;
    ...
    yoshi = 25;
    ....
Wow thanks. What's the need for the "yeehaw & operator" function? The program compiled and reinitialized the private variable fine without it.
Well, show me how you do the statement

yoshi = 25;

without the operator?!

Since the constructor is non-explicit, this will compile even without operator=.
Without an appropriate operator=, when you write
yoshi = 25;
It's as if you had written:
yoshi = yeehaw(25);
In other words, you still create a new (temporary) object every time.
You've misunderstood. I meant the public function in the yeehaw class:

1
2
3
4
5
            yeehaw & operator =( int red )
            {
                ketchup = red;
                return ( *this );
            }


The program compiled fine and yoshi was updated just fine without the function. I don't know how to do it without the equal operator you just showed me. I played around with that function and couldn't figure out for the life of me what it did...could you shed some light on it?

@Athar. That was my initial problem. After I declared an object from yeehaw: yeehaw yoshi(25), it wouldn't let me do it again
Last edited on

@Athar
Since the constructor is non-explicit, this will compile even without operator=.
Without an appropriate operator=, when you write


I did not take into account that there is a conversion constructor.:) I thought that there is only the default constructor.
You've misunderstood.

No, you did. I explained why it works without operator=(int).
The compiler provides an assignment operator that allows you to assign a yeehaw object to another yeehaw object automatically, in case you were not aware.

I don't know how to do it without the equal operator

It's the assignment (copy) operator, not equal operator.

Normally, to change a private member, you use a setter:
void setKetchup(int newKetchup) {ketchup=newKetchup;}

In order to avoid accidental conversions, you should make constructors with one parameter explicit:
explicit yeehaw(int a) : ketchup(a) {}
Last edited on
sorry about the confusion


edit: @athar...Thanks. Honestly this concept is still murky.

I will use a setter like you said earlier but just to clarify....The only way I know how
to update a private member variable via a constructor is when you initially declare
an object and initialize a value:

yeehaw yoshi(42);

and how Vlad from moscow just showed me:

yoshi = 42;

So the equal operator does nothing? When I tried to update the private variable like
this:

yoshi(99);

it gave me an error.

Last edited on
A constructor initializes a new object. You cannot call it on an existing object.

So the equal operator does nothing?

It's still the assignment operator. And obviously it does do something.
Topic archived. No new replies allowed.