string initialization on class inheritance

Started by
5 comments, last by synthexp 22 years, 9 months ago
after countless programming, i still couldn''t understand how character strings works. my problem is with the char* initialization. here''s the problem class A is the superclass in which protected TCHAR* m_str is a member in it and class B is the class that inherits the properties of class A. in the constructor of A, m_str is intialized to _T("Window App") by the means of m_str = _T("Window App"); // first init then in the class b, m_str is overridden by this property m_str = _T("Custom App"); // override (second init) it is my undertanding that the base class is going to be initialized first but the question here, can the m_str be overridden by the exact statement in class B constructor? Can that kind of strings be initialized twice? i''ve tried my own experimentation which goes as the following TCHAR* str = _T("I live in Malaysia.."); // first init then in the following statement.. str = _T("the land of software pirates"); // reinit and i got runtime error. the base case is the same here, i think although the method seems slightly differ. am i missing something here? it''s not that i''m losing sleep over this thing, just to clear up a few blocks in my mind everytime i''m using strings in my app.
Regards,synthexp
Advertisement
I think I know what you''re talking about, and there should be absolutely no problem. Just to make sure, I tried this in VC++:

  class class_a{    public:        class_a() {m_str = "Window App";}    protected:        char* m_str;};class class_b: public class_a{    public:        class_b() {m_str = "Custom App";}};void main(){    class_b b;}  


I encountered no problems whatsoever. You will encounter problems if you try to assign something to that string that isn''t a string literal in your code. For example, if you somehow obtain a string from the user and try to store it using m_str that''ll give you an error. This is because m_str just points to the first character of some string in memory. But since it''s just a pointer, it has no space to actually store a string. So when you''re doing you''re "initialization," you''re making that pointer point to a string that''s allocated in memory. But if you''ll need to store a string, you''ll need to make m_str an array (or, better yet, a std::string). Once you do that, you''ll need to use the strcpy to store an initial string into that array. If you don''t quite understand what I said, forgive me; you probably do need m_str to be an array (or just make it an std::string so you don''t have to worry about pointer nonsense).

I hope some of that helps.
first of all thank you merlin9x9 for replying

i forgot to tell you that the first method really works. i didn''t had the opportunity to put that in my previous thread.

since the second method which is the variable str pointed to some memory space containing "I live in Malaysia" and and the second statement which reset the pointer to points to another location which is "a heaven for software pirates". it was during this second statement did it raised a run-time error. i resupply you with the same statement

TCHAR* str = _T("\0"); // null string

str = _T("I live in Malaysia"); // no error, str points to the first char

str = _T("A heaven for software pirates"); // error here


what i''m confused of is the fact that on the class inheritance method, the var m_str first point to the string "Window App" and by the rights of super class, it is initialized first then in the constructor of the sub class, the m_str points to another string which is "Custom App". and this method actually works!. i''ve been using this successfully but i''m at loggerheads on why these two methods resulted two different outputs.

i''m not an uber coder when it comes to pointers to string. i could have used fixed memory loc (arrays) for the strings but it''s all about mastering the concept.i could just adopt the stl now and forget the ancient c style string but i just want to know more closely on how pointer to a string works.

i hope this re-clear the question to my doubt. thank you for reading
Regards,synthexp
Just use std::string and all your problems will fly away like a bird in the sun....
Visit:http://www.evildawncrew.comAll things which are worth beeing done, are worth beeing donw well.
Your politeness is refreshing.

I''m afraid that I don''t understand where it is that you''re putting this "TCHAR* str =" and "str =" stuff. What is str? How does it relate to m_str, the variable that gets initialized differently in the derived class? Could you show me some of your code (specifically, the implementation of the constructors for both classes)?
i''m sorry all

it seems the computer that i was working all these times has something wrong about something unexplained. i tried the code i was explaining on another pc and it worked. same file, same config but different result????this is completely beyond me.

maybe floviel was right. i should try STL.


thank you all for reading
Regards,synthexp
Hey, I mentioned using the STL before he did! I''m just being silly.

I''ll be the first to tell you that computers are b***s**t. That''s the explanation. Anyway, I''m glad things are working now.

This topic is closed to new replies.

Advertisement