why this code has compile error

Discussion in 'C++' started by Chang PilHun, Jul 9, 2004.

  1. Chang PilHun

    Chang PilHun Guest

    #include <iostream>
    using namespace std;


    template<class T>
    class B1
    {
    public:
    void SayHi()
    {
    T* pT = static_cast<T*>(this);
    pT->PrintClassName();
    }
    protected:
    void PrintClassName()
    {
    cout << "This is B1";
    }
    };


    class D1:public B1<D1>
    {
    //No overridden function at all
    };


    class D2:public B1<D2>
    {
    protected:
    void PrintClassName()
    {
    cout << "This is D2";
    }
    }
    ;

    main()
    {
    D1 d1;
    D2 d2;
    d1.SayHi();
    d2.SayHi();
    }



    --------compiler said
    test.cpp: In member function `void B1<T>::SayHi() [with T = D2]':
    test.cpp:43: instantiated from here
    test.cpp:32: `void D2::printClassName()' is protected
    test.cpp:12: within this context


    why?
    I never called this function.
    .... Just.... c++'s philosophy?
     
    Chang PilHun, Jul 9, 2004
    #1
    1. Advertising

  2. "Chang PilHun" <> wrote in message
    news:cclmgf$nlu$...
    > #include <iostream>
    > using namespace std;
    >
    >
    > template<class T>
    > class B1
    > {
    > public:
    > void SayHi()
    > {
    > T* pT = static_cast<T*>(this);
    > pT->PrintClassName();
    > }
    > protected:
    > void PrintClassName()
    > {
    > cout << "This is B1";
    > }
    > };
    >
    >
    > class D1:public B1<D1>
    > {
    > //No overridden function at all
    > };
    >
    >
    > class D2:public B1<D2>
    > {
    > protected:
    > void PrintClassName()
    > {
    > cout << "This is D2";
    > }
    > }
    > ;
    >
    > main()
    > {
    > D1 d1;
    > D2 d2;
    > d1.SayHi();
    > d2.SayHi();
    > }
    >
    >
    >
    > --------compiler said
    > test.cpp: In member function `void B1<T>::SayHi() [with T = D2]':
    > test.cpp:43: instantiated from here
    > test.cpp:32: `void D2::printClassName()' is protected
    > test.cpp:12: within this context
    >
    >
    > why?


    Because its protected.

    > I never called this function.


    Yes you did.

    d2.SayHi();

    calls

    pT->PrintClassName();

    with pT being of type D2*.

    > ... Just.... c++'s philosophy?
    >


    It's the rules of C++, if that is what you mean. My suggestion would be to
    make PrintClassName public, doesn't seem like it could do any harm.

    john
     
    John Harrison, Jul 9, 2004
    #2
    1. Advertising

  3. In article <cclmgf$nlu$>,
    "Chang PilHun" <> wrote:

    > template<class T>
    > class B1
    > {
    > public:
    > void SayHi()
    > {
    > T* pT = static_cast<T*>(this);
    > pT->PrintClassName();
    > }
    > protected:
    > void PrintClassName()
    > {
    > cout << "This is B1";
    > }
    > };


    > class D2:public B1<D2>
    > {
    > protected:
    > void PrintClassName()
    > {
    > cout << "This is D2";
    > }
    > }
    > ;
    >
    > main()
    > {
    > D1 d1;
    > D2 d2;
    > d1.SayHi();
    > d2.SayHi();
    > }


    > --------compiler said
    > test.cpp: In member function `void B1<T>::SayHi() [with T = D2]':
    > test.cpp:43: instantiated from here
    > test.cpp:32: `void D2::printClassName()' is protected
    > test.cpp:12: within this context


    > why?
    > I never called this function.


    Yes, you did; not in main() but in SayHi(). It seems like you want to
    use templates to implement virtual functions; you can do that to an
    extent, but you can't take it quite this far. Let's remove the template
    business and see what you are really doing:

    void SayHi()
    {
    D2* d2 = static_cast<D2*>(this); // OK it your code
    d2->PrintClassName(); // not allowed by access rules
    }

    This is an error because B1 does not have access to protected members
    defined by its subclasses. You can call B1<T>::printClassName() at that
    point in the code, but since it's not virtual, that won't do what you
    want. So you will either need to use virtual functions, a friend
    designation, or make the function public.

    -Chad
     
    Chad J McQuinn, Jul 9, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    925
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,090
    Smokey Grindel
    Dec 2, 2006
  3. Nagaraj
    Replies:
    1
    Views:
    893
    Lionel B
    Mar 1, 2007
  4. Paul Melis

    Why oh why does this NOT give a compile error?

    Paul Melis, Nov 5, 2007, in forum: C Programming
    Replies:
    13
    Views:
    530
    cr88192
    Nov 7, 2007
  5. fAnSKyer
    Replies:
    2
    Views:
    548
    Alf P. Steinbach
    Jun 7, 2009
Loading...

Share This Page