Avatar of graber
graberFlag for United States of America asked on

help with overwriting std::string operator=

is it possible to call base class functions like we do with the constructors? compilers has a conipsion on the first operator member... no problem with the second... any reasons? also tell me what level is this question... Thanks.
#include <string>
using namespace std;
class CWord: public string
{
   public:
      CWord();
      CWord(const string & str);
      CWord(const string & str, size_t pos, size_t n = npos);
      CWord(const char * s, size_t n);
      CWord(const char * s);
      CWord(size_t n, char c);
      CWord & operator=(const CWord & word);
      CWord & operator=(const string & str);
      CWord & operator=(const char * psz);
   protected:
   private:
      char m_cDelimiter;
};
 
doesn't work
CWord & CWord::operator=(const CWord & word)
              :string(word.c_str())
{
   assign(word.c_str());
   return * this;
}
 
works
CWord & CWord::operator=(const CWord & word)
{
   assign(word.c_str());
   return * this;
}

Open in new window

.NET ProgrammingVisual Basic.NETC++

Avatar of undefined
Last Comment
evilrix

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
evilrix

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
evilrix

>> What you refer to here is the constructor initialization
That should have read "constructor initialization list", also known as the "member initialization list".
http://www.devx.com/tips/Tip/15188
http://www.parashift.com/c++-faq-lite/ctors.html

You can, of course, explicitly call other base class methods as any time, see example below. Note, you should NOT call virtual function in either the constructor or destructor as the behavior is undefined.
http://www.artima.com/cppsource/nevercall.html
#include <iostream>
 
class foo
{
public:
	virtual void test()
	{
		std::cout << "foo::test" << std::endl;
	}
};
 
class bar : public foo
{
public:
	void test()
	{
		std::cout << "bar::test" << std::endl;
 
		// Call base class version of test
		foo::test();
	}
};
 
int main()
{
	bar b;
	b.test();
}

Open in new window

ASKER
graber

thanks guys.... will write more either tonight from home or tomarrow.
Gregg
ASKER
graber

thanks evilrix... so the only place that the convention we call "constructor initialization" is just that. It isn't convention for breaking out to call base function before calling your own routine?...  I have been doing this for about 12 years and I'm still learning.. Thanks
Gregg
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
evilrix

>> so the only place that the convention we call "constructor initialization" is just that. It isn't convention for breaking out to call base function before calling your own routine?
Correct, it's purely just for allowing you to invoke explicit constructors on base and members of your class during construction of your class. If you want to call base class functions during a normal function call you just use the fully qualified name as shown above.

>> ...  I have been doing this for about 12 years and I'm still learning.. Thanks
We all are... anyone who thinks they know everything about C/C++ is just kidding themselves. It's like Othello, takes minutes to learn and a lifetime to master :)
http://en.wikipedia.org/wiki/Reversi

-Rx
ASKER
graber

evilrix.  I was just looking at you history.  We have about the same time in the industry.  You must really called this a passion for you.  I've been working for the defense industry for the last 7 years and nothing new ever hits me.  Starting on a project to convert blender created x files in to cnc instructions for creating 3d models and now were learning again :). Thanks guy.  Best to you and yours.
evilrix

>> Best to you and yours.
You too my friend.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.