inheritance
-
in the name of god hello anyone who is reading i have a problem please help me: in the code below is there any way which the class DerivedContainer in every where treat with DrivedMember as m_instance without static_cast? as i have no well access to net if it is possible please send me the answer to sajadsadeghy@gmail.com or if not also thanks. #include "stdafx.h" class BaseMember { public: int testBase; BaseMember() { } }; class DerivedMemer:public BaseMember { public: DerivedMemer() { } void SomeFunction() { } int testDerived; }; class BaseContainer { public: BaseMember* m_instance; BaseContainer() { m_instance=new BaseMember(); } }; class DerivedContainer:public BaseContainer { public: DerivedContainer() { m_instance=new DerivedMemer(); } void TestFunction() { DerivedMemer* pIs=static_cast(this->m_instance); pIs->testDerived=100; } }; int _tmain(int argc, _TCHAR* argv[]) { DerivedContainer der; der.TestFunction(); return 0; } thanks in advance . valhamdolelah
-
in the name of god hello anyone who is reading i have a problem please help me: in the code below is there any way which the class DerivedContainer in every where treat with DrivedMember as m_instance without static_cast? as i have no well access to net if it is possible please send me the answer to sajadsadeghy@gmail.com or if not also thanks. #include "stdafx.h" class BaseMember { public: int testBase; BaseMember() { } }; class DerivedMemer:public BaseMember { public: DerivedMemer() { } void SomeFunction() { } int testDerived; }; class BaseContainer { public: BaseMember* m_instance; BaseContainer() { m_instance=new BaseMember(); } }; class DerivedContainer:public BaseContainer { public: DerivedContainer() { m_instance=new DerivedMemer(); } void TestFunction() { DerivedMemer* pIs=static_cast(this->m_instance); pIs->testDerived=100; } }; int _tmain(int argc, _TCHAR* argv[]) { DerivedContainer der; der.TestFunction(); return 0; } thanks in advance . valhamdolelah
khomeyni wrote:
DerivedMemer* pIs=static_cast(this->m_instance);
This does not compile. Try:
DerivedMemer *pIs = static_cast<DerivedMemer *>(this->m_instance);
You could also use:
DerivedMemer *pIs = (DerivedMemer *) this->m_instance;
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
khomeyni wrote:
DerivedMemer* pIs=static_cast(this->m_instance);
This does not compile. Try:
DerivedMemer *pIs = static_cast<DerivedMemer *>(this->m_instance);
You could also use:
DerivedMemer *pIs = (DerivedMemer *) this->m_instance;
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
in the name of god hello and thanks for your guide but my question is that every time we need the m_instance of class DrivedContainer we must use TestFunction as in the main belowthat it is not good way to do this in every initializing; is there any way to avoid writing TestFunction and casting the m_instance to DrivedMember class object? void TestFunction() { DerivedMemer *pIs = static_cast(this->m_instance); pIs->testDerived=100; } int main(int argc, _TCHAR* argv[]) { DerivedContainer der; der.TestFunction(); return 0; } thanks valhamdolelah
-
in the name of god hello and thanks for your guide but my question is that every time we need the m_instance of class DrivedContainer we must use TestFunction as in the main belowthat it is not good way to do this in every initializing; is there any way to avoid writing TestFunction and casting the m_instance to DrivedMember class object? void TestFunction() { DerivedMemer *pIs = static_cast(this->m_instance); pIs->testDerived=100; } int main(int argc, _TCHAR* argv[]) { DerivedContainer der; der.TestFunction(); return 0; } thanks valhamdolelah
khomeyni wrote:
...every time we need the m_instance of class DrivedContainer...
From where? It can be accessed from another method in
DerivedContainer
, and inmain()
. How are you wanting to get access tom_instance
?"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
khomeyni wrote:
...every time we need the m_instance of class DrivedContainer...
From where? It can be accessed from another method in
DerivedContainer
, and inmain()
. How are you wanting to get access tom_instance
?"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
in main i used : DerivedContainer der; der.TestFunction(); i mean that every time i create a new object of DrivedContainer i must cast m_instance of class DrivedContainer which has been inherited from BaseContainer to DrivedContainer. class BaseContainer { public: BaseMember* m_instance; BaseContainer() { m_instance=new BaseMember(); } }; int main(int argc, _TCHAR* argv[]) { DerivedContainer der; der.TestFunction(); } you said there is another way for this. please say what i must to do? valhamdolelah
-
in main i used : DerivedContainer der; der.TestFunction(); i mean that every time i create a new object of DrivedContainer i must cast m_instance of class DrivedContainer which has been inherited from BaseContainer to DrivedContainer. class BaseContainer { public: BaseMember* m_instance; BaseContainer() { m_instance=new BaseMember(); } }; int main(int argc, _TCHAR* argv[]) { DerivedContainer der; der.TestFunction(); } you said there is another way for this. please say what i must to do? valhamdolelah
khomeyni wrote:
you said there is another way for this. please say what i must to do?
I've not sure if it is the correct way, but one option would be:
class BaseContainer
{
public:
BaseMember *m_instance;BaseContainer() { m\_instance = new BaseMember(); } BaseContainer( DerivedMemer \*d ) { m\_instance = d; }
};
class DerivedContainer : public BaseContainer
{
public:
DerivedContainer() : BaseContainer( new DerivedMemer )
{
}
};void main( void )
{
DerivedContainer der;
BaseContainer bas;
}This is a part of C++ that I've never had to bother/worry about.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
khomeyni wrote:
you said there is another way for this. please say what i must to do?
I've not sure if it is the correct way, but one option would be:
class BaseContainer
{
public:
BaseMember *m_instance;BaseContainer() { m\_instance = new BaseMember(); } BaseContainer( DerivedMemer \*d ) { m\_instance = d; }
};
class DerivedContainer : public BaseContainer
{
public:
DerivedContainer() : BaseContainer( new DerivedMemer )
{
}
};void main( void )
{
DerivedContainer der;
BaseContainer bas;
}This is a part of C++ that I've never had to bother/worry about.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
in the name of god hello and thanks for your guide but suppose that i want to create a function in DerivedContainer and use m_instance there then it is false for it and yet we must cast for each object. example: class DerivedContainer : public BaseContainer { public: DerivedContainer() : BaseContainer( new DerivedMemer ) { } mynew_fun(){//here we need type_cast yet. DerivedMember *nD=static_cast (this->m_instance); } };