×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

Better way to initialize the member variable in constructor.

#include "stdafx.h"
#include <iostream.h>
class Base
{
public:
Base()
{
tt = 8; // make tt always initialized in the base and derived class.
}
int getTT()
{
return tt;
}
virtual void did(){};
void done();
private:
int tt;
};

class Derived : public Base
{
public:
void did()
{
cout << "this is derived class" << endl;
cout << "tt = " << getTT() << endl;
}
private:
};

void Base::done()
{
Base* b = new Derived;
b->did();
delete b;
}


int main(int argc, char* argv[])
{
Base b;
b.done();
return 0;
}
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 问一个C++的问题
    derived.cpp
    #include "Base.h"
    class Derived : public Base
    {
    public:
    void did(){cout << "this is derived class" << endl;
    cout << "tt = " << getTT() << endl;}
    private:
    };

    base.cpp
    #include <iostream.h>
    class Base
    {
    public:
    int getTT() { return tt;}
    void did(){};
    void done();
    private:
    int tt;
    };

    int main()
    {
    Base b;
    b.done();
    return 0;
    }

    void Base::done()
    {
    tt = 8;
    Base* b=new Derived; // ?????????????????
    b->did();
    delete b;
    }

    问题:
    我想在base class内部,定义一个base pointer, 用derived class初始化,同时又让他是这个base class的derived class, 也就是说,初始化完成后,b->did()应该能够能够打印出
    this is derived class
    tt=8

    Base* b=new Derived; //???????????????????????]
    这一句有问题,谁知道应该怎样初始化?
    在base class外面好办,在里面怎么办?行得通吗?

    谢谢
    • Use constractor fuction of class Derived to do initialization.
      • 我不想这么做,想一个初始化就解决问题,行吗?
        • The following is VC win32 console application, which can output what you want.
          #include "stdafx.h"
          #include <iostream.h>
          class Base
          {
          public:
          int getTT()
          {
          return tt;
          }
          virtual void did(){};
          void done();
          private:
          int tt;
          };

          class Derived : public Base
          {
          public:
          void did()
          {
          cout << "this is derived class" << endl;
          cout << "tt = " << getTT() << endl;
          }
          private:
          };

          void Base::done()
          {
          tt = 8; // only base variable was assigned 8
          Base* b=new Derived;
          b->tt = 8; // only through this way, the new instance's tt can be initialized.
          b->did();
          delete b;
          }


          int main(int argc, char* argv[])
          {
          Base b;
          b.done();
          return 0;
          }
          • 和我现在用的一样,不过我正在试图找一种更好的方法
            想一想,如果有很多data member, 岂不是很麻烦。
            不过也许这是唯一的方法
        • Another way, only need one member function to copy itself.
          #include "stdafx.h"
          #include <iostream.h>

          class Base
          {
          public:
          int getTT()
          {
          return tt;
          }
          virtual void did(){};
          void done();

          Base *pBase;

          protected:
          void membercopy(Base *p)
          {
          tt = p->tt;
          // and so on...
          }
          private:
          int tt;
          };

          class Derived : public Base
          {
          public:
          Derived(Base *p)
          {
          membercopy(p);
          }
          void did()
          {
          cout << "this is derived class" << endl;
          cout << "tt = " << getTT() << endl;
          }
          private:
          };

          void Base::done()
          {
          tt = 8;
          Base* b = new Derived(this);
          b->did();
          delete b;
          }


          int main(int argc, char* argv[])
          {
          Base b;
          b.done();
          return 0;
          }
          • 太复杂了,感觉还不如直接给data member扶植
      • Better way to initialize the member variable in constructor.
        #include "stdafx.h"
        #include <iostream.h>
        class Base
        {
        public:
        Base()
        {
        tt = 8; // make tt always initialized in the base and derived class.
        }
        int getTT()
        {
        return tt;
        }
        virtual void did(){};
        void done();
        private:
        int tt;
        };

        class Derived : public Base
        {
        public:
        void did()
        {
        cout << "this is derived class" << endl;
        cout << "tt = " << getTT() << endl;
        }
        private:
        };

        void Base::done()
        {
        Base* b = new Derived;
        b->did();
        delete b;
        }


        int main(int argc, char* argv[])
        {
        Base b;
        b.done();
        return 0;
        }
        • 不好用呀,小伙
      • 在VC6,怎样操作EXECL表格?那位DX能指点一下!
    • See whether this implement what you want:
      derived.cpp
      #include "Base.h"
      class Derived : public Base
      {
      public:
      void did(){cout << "this is derived class" << endl;
      cout << "tt = " << getTT() << endl;}
      private:
      };

      base.cpp
      #include <iostream.h>
      class Base
      {
      public:
      int getTT() { return tt;}
      virtual void did(){};
      private:
      int tt;
      };

      int main()
      {
      Base *b = new Derived;
      b->did();
      return 0;
      }
      • 不是我想要的
        我是想在base class 里面,先初始化这个base class的某个data member, 然后定义一个base class pointer, 用一个derived class的object初始化这个指针,初始化的同时,让这个drived class的data member同时也被符值(等于base class中这个data member的值)。

        亮晶晶的想法是对的,可是我在derived class CONSTRUCTOR里无法得到 base class的data member 的值?(因为这个base class 和drived class之间没有继承关系)除非传进去一个this, 这样太复杂了。
        • 自寻烦恼
        • 不理解?
          你这样做地目的是什么?
          实际上派生类初始化之前是会自动调用基类构造函数的,你在基类里的tt的值应该能够继承下来. 但是如果你的基类比较复杂,还是用copy constructor吧.
    • Following is my answer, i put all the code in one file and use VC++6.0.
      #include <iostream>
      using namespace std;
      class Derived;

      class Base
      {
      public:
      int getTT() { return tt;}
      virtual void did(){};
      void done();
      protected:
      int tt;
      };

      class Derived : public Base
      {
      public:
      Derived(int aa=0) { tt = aa; }
      void did(){cout << "this is derived class" << endl;
      cout << "tt = " << getTT() << endl;}
      private:
      };

      void Base::done()
      {
      tt = 8;
      Base* b=new Derived (tt); // ?????????????????
      b->did();
      delete b;
      }

      int main()
      {
      Base b;
      b.done();
      return 0;
      }
      • 这个答案比较好,如果有不止一个data member是不是有更好的方法?谢谢
        • give you a hand
          "我想在base class内部,定义一个base pointer, 用derived class初始化,同时又让他是这个base class的derived class"
          FYI:
          1.using constructor
          If you have few members.
          2.using copy constructor
          which can handle whatever data members you need,but it 's not always suitable.
          BTW,Your code is typically bad practice in OO programming.
        • try search "c++ down cast" in deja.com
        • 如果不止一个data member, 我认为处理方法完全一样. 只是构造函数带不止一个参数而已.
    • 我个人认为C++ PROGRAMMERS 应该学会研究一些简单且实用的C++ 反汇编代码.你将更好理解它的精髓.
      • 请问你有没有反汇编的工具?如果有,给我一个. 我的地址: smile2u2u@hotmail.com Thanks in advance.
        • Visual C++ is perfect 反汇编的工具.
      • 我认为没必要.
        • 理解一些底层的东西有助于COMPLICATED CONCEPT的理解.FOR EXAMPLE 通过汇编你将觉得C++多态是如此的容易和如此的具有创新性.
          理解一些底层的东西有助于COMPLICATED CONCEPT的理解.FOR EXAMPLE 通过汇编你将觉得C++多态是如此的容易和如此的具有创新性.

          Hope every c++ programmer to agree with my views.I believe you'll get good benefits by the way.