|
Hi Stephen,
In your code, you are invoking Func from class Derived. What I mean is you can not utilize Func from class Base. Right?
regards,
George
|
|
|
|
|
George_George wrote: In your code, you are invoking Func from class Derived. What I mean is you can not utilize Func from class Base. Right?
A class with a pure-virtual function can not be instantiated; no more and no less. As this example shows Base can still call it:
#include "stdafx.h"
#include <iostream>
using namespace std;
class Base
{
public:
void CallItForMe()
{
Base::Func();
}
virtual void Func() = 0
{
cout << "Base::Func()" << endl;
}
};
class Derived : public Base
{
public:
virtual void Func()
{
cout << "Derived::Func()" << endl;
}
};
int main(int argc, char* argv[])
{
Derived d;
d.CallItForMe();
return 0;
}
Steve
|
|
|
|
|
Hi Stephen,
Your code is wrong. I am discussing __interface, and if you change your code from class Base to __interface Base, your code can not compile.
Any comments?
regards,
George
|
|
|
|
|
George_George wrote: Your code is wrong. I am discussing __interface, and if you change your code from class Base to __interface Base, your code can not compile.
The same principles apply. Try this if you prefer:
#include "stdafx.h"
#include <iostream>
using namespace std;
class Base
{
public:
virtual void Func() = 0
{
cout << "Base::Func()" << endl;
}
};
class Derived : public Base
{
public:
virtual void Func()
{
cout << "Derived::Func()" << endl;
}
};
int main(int argc, char* argv[])
{
Derived d;
d.Base::Func();
return 0;
}
Steve
|
|
|
|
|
Try what, Steve?
If you change code from class Base to __interface Base, your code can not compile. Your above code still use class other than __interface.
regards,
George
|
|
|
|
|
The compiler I using now doesn't support the __interface keyword, but perhaps this is more to your liking:
#include "stdafx.h"
#include <iostream>
using namespace std;
__interface Base
{
void Func()
{
cout << "Base::Func()" << endl;
}
};
class Derived : public Base
{
public:
virtual void Func()
{
cout << "Derived::Func()" << endl;
}
};
int main(int argc, char* argv[])
{
Derived d;
d.Base::Func();
return 0;
}
Steve
|
|
|
|
|
It is ok, Steve!
I have tested that your code can compile and generates the expected result. I think it really conflicting to what you have said before -- even if you add body to pure virtual function body, the body does not take any effect or something like this.
Any comments?
regards,
George
|
|
|
|
|
For the last time, for this is getting receptive:
The pure-virtual ness of a function and whether or not it contains a body are independent issues. The body, if present, is not ignored by the compiler, be the function virtual or pure-virtual . I posted code that explicitly demonstrated this.
Steve
|
|
|
|
|
Thanks Steve,
I have got your point, which is, in __interface we can put function body and implement and non-pure virtual function, and the function body is not ignored by compiler. Right?
Just a little confused,
"The pure-virtualness of a function and whether or not it contains a body are independent issues."
Why do you say it is independent issue?
regards,
George
|
|
|
|
|
George_George wrote: I have got your point, which is, in __interface we can put function body and implement and non-pure virtual function, and the function body is not ignored by compiler. Right?
Yes, as the examples I've given have shown.
George_George wrote: "The pure-virtualness of a function and whether or not it contains a body are independent issues."
Why do you say it is independent issue?
If a class has functions that are pure-virtual it just means the compiler will refuse to instantiate it, if the function has a body or not is of no bearing.
Steve
|
|
|
|
|
Thanks Steve,
My question is answered. It is good to learn from you that a pure-virtual function could have body and in some situations we could take use of it.
regards,
George
|
|
|
|
|
I think u can call that function using Scope resolution
|
|
|
|
|
No Rajkumar, you can not.
Here is my code and compile error. Any comments?
error C2352: 'IFoo::goo' : illegal call of non-static member function
__interface IFoo {
int goo() {return 0;}
};
int main()
{
cout << IFoo::goo() << endl;
return 0;
}
regards,
George
|
|
|
|
|
That call would only be valid if the method was static ! On a side note virtual functions can't be static (it doesn't make sense).
Steve
|
|
|
|
|
I agree Steve, I have tried Rajkumar's solution will cause compile error.
regards,
George
|
|
|
|
|
Rajkumar is correct.
Steve
|
|
|
|
|
Where is the object man, goo is not a static function to call like.
I would recall steve's example and modify like this,
Derived d.
d.Base::Func(); // scope resolution
|
|
|
|
|
I changed my mind, Rajkumar! You are correct and Steve's sample is wrong. He should use __interface other than class.
__interface Base
{
public:
virtual void Func() = 0 { cout << "Base::Func()" << endl; }
};
class Derived : public Base
{
public: virtual void Func() { cout << "Derived::Func()" << endl; }
};
int main(int argc, char* argv[])
{
Derived d;
d.Base::Func();
return 0;
}
regards,
George
|
|
|
|
|
We need a George_George message board.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
Definitely.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
I don't want to argue much on the subject, because i'm simply not an expert about "C++ Interfaces".
and the designation "C++ Interface" is just a paradox and should **NOT** be employed like that.
the C++ Standard doesn't even talk about interfaces.
this is a pure invention of Microsoft and Visual C++ Compiler.
So i'd advise you to be very careful with'em.
that been said, why do you keep argueing with MVPs as if you were better than them ?
the way you reply to Cedric, Stephen and CPallini is not very nice of you, you you should be happy they still answer your weird questions.
|
|
|
|
|
Thanks toxcct!
Previously I think according to MSDN standard __interface should contain only pure virtual methods, but I read some code which contains implemented method in __interface, which makes me confused about my previous knowledge. This is the initiative of why I am asking this question.
regards,
George
|
|
|
|
|
but remember, MSDN is microsoft, thus not standard.
so, MSDN only tells how the Microsoft C++ compiler will understand a C++ code. It never tells that this code is C++ standard. to understand this well, try to compile your code under gcc. you gonna pull some hair !!
|
|
|
|
|
Hello everyone,
Here is section of code I wrote about AddRef and Release. My questions are,
1. Are they correct code?
2. It looks redundant to call so many times of AddRef and Release just to make the counter consistent, if you were me, do you have ways to write better code?
[Code]
IX* pI = NULL;
pI = pI1;
pI -> AddRef();
...
pI -> Release(); // release before binding to a new interface
pI = pI2;
pI -> AddRef();
...
pI -> Release(); // release before binding to a new interface
pI = pI3;
pI -> AddRef();
...
[/Code]
thanks in advance,
George
|
|
|
|
|
Hello George_George,
Try smart pointers like _com_ptr_t and CComPtr. These are C++ classes which function as wrappers around actual interface pointers.
Each wrapper stores an actual interface pointer internally. AddRef() and Release() are called automatically as you perform assignment and copy operations.
- Bio.
|
|
|
|