I have a base class named A containing a string type parameter.
Class B is derived from A.
I define class C have parameter A* a, and
assign it to B.
In the main function, I cannot get the string value of the base class as it became blank when b deconstructs.
I want it to output:
"Hello!"
"Hello!"
end
But the output is:
"Hello!"
end
Here is my code:
class A {
public:
string str;
};
class B : public A {
public:
B(string _str) {
str = _str;
}
};
class C {
public:
A *a;
public:
void printOut() {
B b("Hello!");
a = &b;
cout << a->str << endl;
}
};
int main() {
C c;
c.printOut();
cout << c.a->str << endl;
cout << "end" << endl;
return 0;
}
How can I deal with it?
Correct, because B b("Hello!"); goes out of scope, c.a is now a dangling pointer that will cause undefined behaviour upon being dereferenced. If you want it to outlive the scope you could allocate it on the heap instead :
class A {
public:
string str;
};
class B : public A {
public:
B(string _str) {
str = _str;
}
};
class C {
public:
A *a;
public:
void printOut() {
B* b = new B("Hello!");
a = b;
cout << a->str << endl;
}
};
int main() {
C c;
c.printOut();
cout << c.a->str << endl;
cout << "end" << endl;
delete c.a;
return 0;
}
This gets messsy real fast though because you have to track the memory allocated yourself and call delete appropriately, consider redesigning or using smart pointers.
Why are you storing an A* ? You know that doesn't work, so stop doing it.
Make a copy of the A object, or a copy of the string it contains, and stop trying to do something silly.
class A {
public:
string str;
};
class B : public A {
public:
B(string _str) {
str = _str;
}
};
class C {
public:
string str;
public:
void printOut() {
B b("Hello!");
str = b.str;
cout << str << endl;
}
};
int main() {
C c;
c.printOut();
cout << c.str << endl;
cout << "end" << endl;
return 0;
}
Related
I'm writing some code to show inheritance.
In doing so, i want to illustrate it by having a base class that contains a vector of pointers that can hold object pointers of the derived class.
I'm getting this error that the "Child class is undeclared" in the base function "void addChild(string nm, string sm)" in the Parents class (base class). I do understand that it maybe out of scope in the base class.
Can someone provide me with a solution to this where i can still be able to instantiate an object of the derived class from within the base class.
I want to have everything done within the base class.
Please clarify if this is ok and is a good practice. If not, please suggest some ideas.
Here's my code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Parents // base class
{
vector <Parents*> fam;
protected:
string firstName;
string lastName;
public:
Parents()
{
//default constructor
}
Parents(string fn, string ln)
{
firstName = fn;
lastName = ln;
}
void displayChildren()
{
if (fam.empty())
{
cout << "Vector is empty" << endl;
}
else
{
for (unsigned int i = 0; i < fam.size(); i++)
{
std::cout, fam.at(i);
}
}
}
void displayParentsInfo(Parents& const par)
{
cout << "First name : " << par.firstName << endl;
cout << "Last name : " << par.lastName << endl;
}
void addChild(string nm, string sm)
{
Child* c1 = new Child(nm, sm);
fam.push_back(c1);
}
};
class Child : public Parents //derived class
{
string firstname;
string surname;
public:
Child()
{
//default constructor
}
Child(string a, string b)
{
firstname = a;
surname = b;
}
//~Child()
//{
//destructor called
//}
void displayChildInfo(Child & const c)
{
cout << "Child's firstname : " << c.firstname;
cout << "Child's surname : " << c.surname;
}
};
Cheers!
Just move the definition of the function out of the definition of the class:
class Parents // base class
{
...
void addChild(string nm, string sm);
};
class Child : public Parents //derived class
{
...
};
void Parents::addChild(string nm, string sm)
{
Parents* c1 = new Child(nm, sm);
fam.push_back(c1);
}
As for good practice, it might be better to have a non-member function that prepares the Child and returns a pointer to it, and add something like:
void Parents::addToFam(Parents* c1)
{
fam.push_back(c1);
}
Lets say I have implemented the following classes
class A
{
public:
virtual void printA()
{
cout << "Hi from A!" << endl;
}
};
class B : public A
{
public:
virtual void printB()
{
cout << "Hi from B!" << endl;
}
};
class C : public B
{
public:
void printC()
{
cout << "Hi from C!" << endl;
}
};
Lets also say I have created a std::vector<A *> vec that contains random amount of objects instantiated from A, B, and C. Now lets say I am forced to iterate through all the objects in vec but only call objects that have the printC() method (i.e C instances). What is the best way to do this?
int main()
{
std::vector<A *> vec;
....
// insert random objects from both A, B and C into vec
....
for(vector<A *>::iterator x = vec.begin();
x != vec.end();
x++)
{
if(dynamic_cast<C *>(*x) != 0) // 1. is this OK?
(*x)->printC();
else
(*x)->printA(); // 2. is this also OK?
}
}
Is 1 Ok? And if so is this the best practice?
Also will 2 cause problems in the case of C instances?
Maybe these are stupid questions, but Im quite new to C++ and how polymorphism works in C++ is very strange to me. Thanks
I think you mean the following
#include <iostream>
#include <vector>
int main()
{
class A
{
public:
virtual ~A() = default;
virtual void print() const
{
std::cout << "Hi from A!" << std::endl;
}
};
class B : public A
{
public:
void print() const
{
std::cout << "Hi from B!" << std::endl;
}
};
class C : public B
{
public:
void print() const
{
std::cout << "Hi from C!" << std::endl;
}
};
std::vector<A *> v = { new A(), new B(), new C() };
for ( A *p : v ) p->print();
return 0;
}
The output is
Hi from A!
Hi from B!
Hi from C!
1 won't work, since *x has type A*, and A doesn't have a printC member. It should be:
if (C * c = dynamic_cast<C *>(*x)) {
c->printC();
}
2 is fine, but doesn't match your description; you say you want to "only call objects that have the printC() method", while this calls printA() on the other objects.
This does seem like an odd design though; you'd usually define a single virtual function, implemented by each class to do the right thing for that class, then call that unconditionally for everything.
For example, I have a base class A and its derived classes B, C, and so on. I have data with a pointer pointing to A. It might be new B, new C, and so on. Any easy way to write and read the pointer to/from a stream? My question is on how to get to know the concrete type. An example to show what I mean.
struct A { int i; };
struct B : public A { char c; };
struct C : public A { float f; }
struct Data
{
unique_ptr<A> mA;
};
Data data;
User works on data and then write out to a file and read in from the file.
The answer is you don't, you use virtual functions.
#include <iostream>
struct A {
int i;
virtual void describe() {
std::cout << "A:" << i << std::endl;
}
};
struct B : public A {
char c;
virtual void describe() override {
// Assume a 'B' wants to also output the A stuff.
std::cout << "B:" << c << ":";
A::describe();
}
};
struct C : public B {
float f;
virtual void describe() override {
// Assume a 'C' wants to also output the B stuff and A stuff.
std::cout << "C:" << f << ":";
B::describe();
}
};
#include <vector>
int main() {
std::vector<A*> bar;
A a;
a.i = 10;
B b;
b.i = 22;
b.c = 'b';
C c;
c.i = 5;
c.c = 'X';
c.f = 123.456;
bar.push_back(&a);
bar.push_back(&b);
bar.push_back(&c);
for (size_t i = 0; i < bar.size(); ++i) {
bar[i]->describe();
}
}
http://ideone.com/12BEce
It's hard to explain exactly what I want to do here, but I have a base class and two classes which inherit this base class. Both classes which inherit it have their own unique members. I want to be able to pass both to a method, and have that method detect which it is, then access their unique members. I can't assume there will only be two classes which inherit it, so i'm looking for something of a more general solution.
Here is an example of what I'd like to do:
#include <iostream>
class Base {
public:
int _type;
Base() { }
};
class First : public Base {
public:
int _first_only;
First() { }
};
class Second : public Base {
public:
int _second_only;
Second() { }
};
void test (Base b) {
std::cout << "Type: " << b._type << std::endl;
if(b._type==1) {
std::cout << "First\n";
// Want to be able to do this
std::cout << "Val: " << (First)b._first_only << std::endl;
} else if(b._type==2) {
std::cout << "Second\n";
// And this
std::cout << "Val: " << (Second)b._second_only << std::endl;
}
}
int main() {
First f;
f._first_only=1;
f._type=1;
Second s;
s._type=2;
s._second_only=2;
test(f);
test(s);
}
This is similar to others answers:
You can write polymorphic classes to get this behavior using virtual functions.
Pass the Dervied class objects either by pointer or by reference to get polymorphic behaviour. Otherwise it will lead to object slicing. Your test() function leads to object slicing.
This code may also help you. You can see that there are different ways to print the type. I used GetBaseType(), GetDerivedType() and GetType(). Among these GetType() method is convenient for you case. There are two constructors for convenience. Constructors allow to initialize data members.
class Base {
private:
int _type;
public:
Base(int type) : _type(type) { }
int GetBaseType() { return _type; }
virtual int GetDerivedType() = 0;
virtual int GetType() { return _type; }
};
class First : public Base {
private:
int _first_only;
public:
First() : Base(1), _first_only(1) { }
First(int first_only) : Base(first_only), _first_only(first_only) { }
int GetDerivedType() { return _first_only; }
virtual int GetType() { return _first_only; }
};
class Second : public Base {
private:
int _second_only;
public:
Second() : Base(2), _second_only(2) { }
Second(int second_only) : Base(second_only), _second_only(second_only) { }
int GetDerivedType() { return _second_only; }
virtual int GetType() { return _second_only; }
};
void test (Base &b) {
std::cout << "Type: " << b.GetBaseType() << std::endl;
std::cout << "Type: " << b.Base::GetType() << std::endl;
std::cout << "Dervied type: \n";
std::cout << "Val: " << b.GetDerivedType() << std::endl;
std::cout << "Val: " << b.GetType() << std::endl;
}
int main() {
First f(1);
Second s(2);
test(f);
test(s);
First f1;
Second s1;
test(f1);
test(s1);
}
Either declare a virtual function in Base
Move the common members types from First and Second into Base.
For your specific problem, 2nd option is better:
class Base {
public:
int _member; // have getter() method, if '_member' is private
Base() { }
};
Inside, test():
void test (Base &b) { // <--- practice to pass by reference if copy is not needed
// use b._member;
};
Your code does not work polymorphically, because you are passing the function-parameter by value, which results in slicing.
If you have a method that does different things for different types, consider overloading it for each of these types.
Three things I'd do:
In general switching on type codes is not considered good object oriented design: Instead pull the switched code into the classes.
I'd also set up the type tags in the constructor of the specific classes.
And as others have mentioned you need to pass the argument by reference to avoid slicing.
Here's what the code would look like:
#include <iostream>
class Base {
public:
int _type;
Base() { }
virtual void print_to_stream( std::ostream & os ) const =0;
};
class First : public Base {
public:
int _first_only;
First() { _type =1; }
void print_to_stream( std::ostream & os ) const
{
os<<"First\n";
os<<"Val: " << _first_only << std::endl;
}
};
class Second : public Base {
public:
int _second_only;
Second() { _type=2; }
void print_to_stream( std::ostream & os ) const
{
os << "Second\n";
os << "Val: " << _second_only << std::endl;
}
};
void test (Base & b)
{
std::cout << "Type: " << b._type << std::endl;
b.print_to_stream( std::cout );
}
int main() {
First f;
f._first_only=1;
Second s;
s._second_only=2;
test(f);
test(s);
}
I have an existing application in C++ with a custom ArrayBase class that manages storage and access to a contiguously allocated region of memory. I have a separate ItrBase class that is used to access data in that ArrayBase. ArrayBase has a createItr() function that currently returns an ItrBase object.
I need to extend ArrayBase to use multiple memory allocations instead of one contiguous one. I have created an EnhancedArray class to do that. For this EnhancedArray to be compatible with the existing application, it's createItr() function must return something that works with the new multiple memory allocations.
So, I have created a derived EnhanceItr class to do this.
My problem is I can't figure out a way for hundreds of code occurrences like this:
ItrBase anIterator = anArray.createItr();
...
double x = anIterator.getData();
to use the EhancedItr's getData() function when anArray is an EnhancedArray.
Here is a simple application illustrating my basic arrangement.
#include <iostream>
using namespace std;
class ItrBase {
public:
ItrBase() { cout << "ItrBase constructor.\n"; };
~ItrBase() { cout << "ItrBase destructor.\n"; };
virtual int vfunc() {return 1;};
};
class EnhancedItr : public ItrBase {
public:
EnhancedItr() { cout << "EnhancedItr constructor.\n"; };
~EnhancedItr() { cout << "EnhancedItr destructor.\n"; };
int vfunc() {return 0;};
};
class ArrayBase {
public:
ArrayBase() { cout << "ArrayBase constructor.\n"; };
~ArrayBase() { cout << "ArrayBase destructor.\n"; };
virtual ItrBase & createItr() {cout << "in AB's createItr()\n"; return *new ItrBase(); };
};
class EnhancedArray : public ArrayBase {
public:
EnhancedArray() { cout << "EnhancedArray constructor.\n"; };
~EnhancedArray() { cout << "EnhancedArray destructor.\n"; };
EnhancedItr & createItr() {cout << "in EA's createItr()\n"; return *new EnhancedItr(); };
};
int main()
{
ArrayBase ab;
EnhancedArray ea;
ItrBase itr = ab.createItr();
ItrBase eitr = ea.createItr(); //EnhancedItr assigned to ItrBase
cout << "ArrayBase's Itr .vfunc(): " << itr.vfunc() <<std::endl;
cout << "EnhancedArray's Itr .vfunc(): " << eitr.vfunc() <<std::endl;
return 0;
}
Both calls to vfunc() above return 1, when I want the second call to return 0.
In main(), I know that if I change the ItrBase types to ItrBase &'s, I do get the desired return types, but then I am modifying my 'existing' code in hundreds of areas, and the destructors for the Iterators are not called.
Is there another strategy that I am not seeing?
Thanks.
Sure, if you're allowed to rewrite ItrBase, then you can use delegation to pass all function calls through to an implementation class, which you hold by pointer or reference so that polymorphism is in effect. This would look a lot like pimpl. And the callers would not have to be written at all, only recompiled.
EDIT: code for those not familiar with pimpl.
struct ItrBase
{
struct ItrImpl
{
virtual ~ItrImpl(){}
virtual int vfunc() = 0;
};
ItrBase(ItrImpl peer) : m_peer(peer) { cout << "ItrBase constructor.\n"; }
~ItrBase() { cout << "ItrBase destructor.\n"; }
int vfunc() { return m_peer->vfunc(); }
private:
const unique_ptr<ItrImpl> m_peer;
};
class ArrayBase
{
struct ItrImpl : public ItrBase::ItrImpl
{
virtual int vfunc() { return 0; }
};
public:
ArrayBase() { cout << "ArrayBase constructor.\n"; };
~ArrayBase() { cout << "ArrayBase destructor.\n"; };
virtual ItrBase createItr() { cout << "in AB's createItr()\n"; return ItrBase(new ItrImpl); };
};
class EnhancedArray : public ArrayBase
{
struct ItrImpl : public ItrBase::ItrImpl
{
virtual int vfunc() { return 1; }
};
public:
EnhancedArray() { cout << "EnhancedArray constructor.\n"; };
~EnhancedArray() { cout << "EnhancedArray destructor.\n"; };
virtual ItrBase createItr() { cout << "in EA's createItr()\n"; return ItrBase(new ItrImpl); };
};
You're running into a problem called slicing: createItr returns a reference, and then you're copying that into an ItrBase by-value. It's as if you did something like this:
EnhancedItr itr1 = ...;
BaseItr itr2 = itr1; // copy by-value
cout << itr2.vfunc(); // prints 1, not 0
You're also leaking memory: createItr returns a newly allocated object, but you're never deleting it. This is very bad, especially since you'd expect array iterators to be used frequently.
completely different thing you can do is use,
BOOST_AUTO(iterator, array);
and let compiler figure out return type.
BOOST_AUTO
Not being up-to-date with the Standard Library, I could not use the unique_ptr<> implementation suggested by Ben Voigt. (version >=4.3) I believe I have taken his concept and implemented it with basic pointers instead. Noting, however, that this implementation is not exception-safe. ItrImpl objects could be left undeleted.
Here's my code. Too bad createItr() has to return a ItrBase object rather than a pointer, otherwise I think I could have gotten auto_ptr<> to work. Output during program execution shows that ~ItrBase() is called only once for each instance, but I am suprised it is not called also during the object return from createItr(). Return value optimization?
#include <iostream>
using namespace std;
struct ItrBase
{
struct ItrImpl
{
virtual ~ItrImpl(){};
virtual int vfunc() const = 0;
};
ItrBase(ItrImpl* peer) : m_peer(peer) { cout << "ItrBase constructor.\n"; };
~ItrBase() { cout << "ItrBase destructor. \n"; delete m_peer; };
int getData() const { return m_peer->vfunc(); };
private:
ItrImpl* const m_peer;
};
class ArrayBase
{
struct ItrImpl : public ItrBase::ItrImpl
{
virtual int vfunc() const { return 0; };
};
public:
ArrayBase() { cout << "ArrayBase constructor.\n"; };
~ArrayBase() { cout << "ArrayBase destructor.\n"; };
virtual ItrBase createItr() { cout << "in AB's createItr()\n"; return ItrBase(new ItrImpl); };
};
class EnhancedArray : public ArrayBase
{
struct ItrImpl : public ItrBase::ItrImpl
{
virtual int vfunc() const { return 1; };
};
public:
EnhancedArray() { cout << "EnhancedArray constructor.\n"; };
~EnhancedArray() { cout << "EnhancedArray destructor.\n"; };
virtual ItrBase createItr() { cout << "in EA's createItr()\n"; return ItrBase(new ItrImpl); };
};
int main()
{
ArrayBase ab;
EnhancedArray ea;
ItrBase itr = ab.createItr();
ItrBase eitr = ea.createItr(); //EnhancedItr assigned to ItrBase
cout << "ArrayBase's Itr .vfunc(): " << itr.getData() <<std::endl;
cout << "EnhancedArray's Itr .vfunc(): " << eitr.getData() <<std::endl;
return 0;
}