[SOLVED] Can we access a class attribute by the class name in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

[SOLVED] Can we access a class attribute by the class name in C++?

I am currently working on something which requires extensive use of string manipulation. So I thought of writing my own library for strings (main aim was to mould the in-built functions of the string class to my needs). So this question arose in my mind that can we access an attribute of a class using the class name itself. For example, in my class (called String), I have a public member called str (of type std::string). So suppose that a variable of the defined type String is declared. I want String new_string = name_of_old_string_variable; //operator= is already defined To be compiled as String new_string = name_of_old_string_variable.str; I know that I'm not good with words, so you can check out the code for better understanding of what I want to do. https://code.sololearn.com/cxqUAE1EJVea/?ref=app

18th Mar 2020, 11:03 AM
XXX
XXX - avatar
7 Answers
+ 5
XXX , Something like this? Edit: I have also overloaded stream insertion operator just to show assignment is working. https://code.sololearn.com/cTEfKDKXT4Kg/?ref=app
18th Mar 2020, 11:38 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 2
Not sure I understand it, doesn't std::string old_string = ...; String new_string = old_string; already compile correctly? Apart from not returning *this. ( using std:: to make it clear which string it is ) And it does as well if you replace std::string with String and replace the '= ...' with { ... }
18th Mar 2020, 11:18 AM
Dennis
Dennis - avatar
+ 2
I know it doesn't say std::string new_string = old_string. I was just thinking you meant replacing std::string with String because of the comment. I'm still not completely sure what you mean because String old_string{"abcd"}; String new_string = old_string; already compiles. You may want to define a String( const char* ) to do String old_string = "abcd"; though.
18th Mar 2020, 11:31 AM
Dennis
Dennis - avatar
+ 2
I see, then you probably want to define the assignment operator: String& operator= ( const String& x){ this->str = x.str; return *this; } like in Omkar's example ( don't forget to return String& otherwise chained assignments won't work ) And the constructor equivalent: String( const String& x ) : str( x.str ) {} Though you may have to decide what to do with the 'other attributes' here, since they'll be uninitialized.
18th Mar 2020, 11:48 AM
Dennis
Dennis - avatar
+ 2
🇮🇳Omkar🕉 oh damn!! I can't believe I didn't think of that. Thanks for pointing out. Thanks to Dennis too. I just wasted my time. I don't know why instead of thinking over it, I had supposed it was impossible.
18th Mar 2020, 12:03 PM
XXX
XXX - avatar
0
Dennis look carefully, it's string new_string = old_string; Not String new_string = old_string; And to further clarify, I want to be able to do String = String; And only change the attribute 'str' of the String on the left hand side. Just like we do string = string; (I'll just add std::string everywhere so that there is no confusion between String and string)
18th Mar 2020, 11:24 AM
XXX
XXX - avatar
0
Dennis String new_string = old_string; compiles but overrides the rest of the attributes also. See the code now. (And sorry I'm just bad at explaining)
18th Mar 2020, 11:45 AM
XXX
XXX - avatar