Calling class member function on initilization

I have a very short question, all it needs is a yes or no answer. Although an explanation would be perfectly welcome!

If I have a class, for example, Bank, when I initialize an object of the class Bank, can I call a member function at the same time?

E.g.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

// Bank header file
#include "bank.h"

int main ()
{
    Bank SameDiffBank.login ();
    
    return 0;
}

Given that the login() function is a member function of Bank and that all of the code is error-free.

This is mainly because I'm kind of lazy and I just want to put the whole thing in one line of code.
Thanks!
max
Last edited on
If I have a class, for example, Bank, when I initialize an object of the class Bank, can I call a member function at the same time?
No, unfortunately.

This is mainly because I'm kind of lazy and I just want to put the whole thing in one line of code.

The lazy way is to write two lines of code. You're probably trying to improve your code's appearance. Problem is, that's a waste of time. Your final product's correctness, robustness, and performance is all that matters.

Concision is desirable, but it's of limited benefit here and you'd have to purchase it (by changing your code to accommodate this.) It's not worth it - just write the two lines of code.

One of my favorite essays in CS, Ken Iverson's Notation as a Tool of Thought discusses concison:
https://dl.acm.org/doi/pdf/10.1145/358896.358899
See also this excerpt from Whitehead (shorter):
http://introtologic.info/AboutLogicsite/whitehead%20Good%20Notation.html
Last edited on
Code is much more often read than written so you should aim for clean readable code, especially when you want to read your code and maybe fix it.

Unfortunately many people thinks that shorter code is better code. It might have been true in the dark old ages when compiler where not so efficient, but compiler have improved a lot.

I recommend that people should learn to write clean, readable code.
If neccessary it's easier to make clean code faster than to make a fast mess correct.

A good book about clean C++
https://www.modernescpp.com/index.php/clean-c
Err, cheating!

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

class Bank
{
public:
   static Bank login(){ std::cout << "Boo!";   return Bank(); }
};

int main ()
{
   auto SameDiffBank{ Bank::login() };
}
Last edited on
Since this is the beginner's forum, I'll point out that you can could call login() within the constructor:
1
2
3
4
5
6
7
8
9
10
11
12
class Bank {
public:
    Bank() {
        login();
    }
    void login();
};

int main()
{
    Bank sameDiffBank;
}


However, this will call login() for every Bank object that's constructed, which might not be what you want.
Hmm, many very good points here. I was mainly wanting to experiment to see how short I could make my driver program.

mbozzi,
That makes sense, I usually try to write clean, readable code (which is why I sometimes will beat up on people for using "Egyptian braces"), unless I'm in a big hurry or if I'm just making a driver to test some function or other.

thmm,
That makes sense, too. I usually spend an inordinate amount of time trying to debug programs and fix errors left by bad typing, bad logic, bad code, etc etc etc ;)

lastchance,
Hmm, not exactly what I was looking for, but thanks! LOL, I'm not sure if I can return a Bank object from my login() function...I made it void already, and my banking system uses 6 other header files too, so it would be a pain to change it.

dhayden,
You're right, although I only really need one Bank object. So it could work...
dhayden wrote:
Since this is the beginner's forum,

Primarily out of curiosity, what if this was, say, the General Programming forum? Would you have given a more complex solution?

Anyway, thanks guys!
And have a good day!
max
what if this was, say, the General Programming forum? Would you have given a more complex solution?
No. In the General Programming forum, I would have assumed that you knew you could put the code in the constructor.
Ah. Well, I didn't know that, so thank you!

max
If you are only going to use one Bank object, then that should be a singleton class so that the instantiation of the class is limited to only one object. Every class instantiation then returns the same object.
That makes sense, I usually try to write clean, readable code (which is why I sometimes will beat up on people for using "Egyptian braces"), unless I'm in a big hurry or if I'm just making a driver to test some function or other.

Whether braces are lined up - or even the choice of bank b; b.login(); vs. the imaginary syntax bank b.login() are examples of typographic considerations. Most of us stare at text all day, so while typography is relevant to programmers, it ultimately doesn't have much to do with the quality of the software we deliver.

Goodhart's law implies that readability isn't something to optimize.
https://en.wikipedia.org/wiki/Goodhart%27s_law

Unfortunately many people think that shorter code is better code. It might have been true in the dark old ages when compiler where not so efficient, but compiler have improved a lot.

The number of errors in a code base is roughly proportional to its size. Therefore a smaller codebase will typically contain fewer errors. This implies that compressing the same amount of code into fewer lines does not improve the correctness or robustness of the final product.

Code should use abstractions which make it natural to discuss its task. This almost invariably shortens the code, makes it more understandable and improves the project's results. Readable code will follow.
https://dl.acm.org/doi/pdf/10.1145/358896.358899

If you are only going to use one Bank object

Then IMO one should never have written the class at all.
Last edited on
The number of errors in a code base is roughly proportional to its size. Therefore a smaller codebase will typically contain fewer errors.
@mbozzi,
i was thinking about sth like if-else vs the ternary operator.
I find if-else easier to read though it is a bit longer.
I guess the compiler will create the same assembler code for it.

Topic archived. No new replies allowed.