It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
How should I get rid of the problem with object slicing in c++.
In my application if the derived class has some dynamically allocated pointer and derived class object is assigned to base class object, the behavior is memory corruption!
It depends on your design. You may have to change certain design criteria to get rid of it. One of the options is to have an overloaded operator = and copy constructor in your base class for particular derived class.
class Derived;
class Base
{
//...
private:
Base (const Derived&);
Base& operator = (const Derived&); // private and unimplemented
};
Now if you attempt to do something like following:
Derived d;
Base b;
b = d; // compiler error
it will result in compiler error.
you can't. you should solve the problem with the pointer. if you want to assign Obj2 to Obj1, override assign operator (operator=)
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
How to properly initialize container attribute avoiding reconstructing contained objects?
class BAR
{
...
};
class FOO
{
public:
FOO(FOO &&f)
{
// ????
}
std::vector<BAR> b;
};
Unless you have a good reason for doing otherwise, just follow the Rule of Zero and avoid defining a move constructor explicitly: the compiler will generate one for you implicitly, and that move constructor will perform a member-wise move of the class data members.
If you really have to define a move constructor explicitly (e.g. because you are using MSVC, and for some obscure reason MSVC will never generate a move constructor for you implicitly), do it this way:
Foo(Foo&& f) : b(std::move(f.b)) { /* ... */ }
See Andy's answer, but if you need to:
class FOO
{
public:
FOO(FOO &&f) : b(std::move(f.b))
{
}
std::vector<BAR> b;
};
It would be
FOO(FOO &&f): b(std::move(f.b))
{
}
But it's not necessary, as others have pointed out, it's the implicit move constructor will generate for you.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have following code:
class B{
protected:
X *x;
public:
function(char *data){
// access x
// works fine
}
};
class D: public B {
function2(char *data)
{
// access x
// gets garbage
}
};
I have a member variable which is pointer. moreover this pointer is inside "data" but when i access in class D it shows garbage.
can some body please help me...
I've put on my psychic debugging hat, and come to the conclusion you're probably doing something like this:
class Packet
{
public:
Gizmo* gizmo_;
};
class Processor
{
public:
void ProcessPacket(char* packet);
};
// ...
Packet packet;
// packet filled with goodies
Processor proc;
proc.ProcessPacket(reinterpret_cast<char*>(&packet.gizmo_));
Am I right?
If so, my guess is that the Gizmo you're pointing to has somehow fallen out of scope -- thereby becoming destroyed -- or you performed the cast incorrectly.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
In C++, I want to be able to call a method in the same class without creating an object of the whole class. The class is huge and I do not want to create a huge memory location for an object. I am used to programming in C#.
In C# I could do this
class test()
{
private void A()
{
B();
}
private void B()
{
doSomething;
}
}
in C++ I am under the impression I have to do.
class test()
{
public:
static void A();
void B();
};
void test::A()
{
test t;
t.B();
}
void test::B()
{
doSomething;
}
}
I do not want to make B() static nor do I want to create and object of test because in reality my class is a lot larger than this, and creating a object of the class would use memory that I do not want to.
Is there a way I can accomplish what I could in C# in C++?
No. If B needs an object, you have to give it an object. If B doesn't need an object, declare it static. C# is no different -- in your example, A is not static so the object already exists.
static void A();
void B();
You cannot use static function to call non-static one at all.
Solution:
Mark B as static too (if it doesn't depend on current object) and thus you don't have to creat a new object. Else I think A should be non-static.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
class A{};
class B : A{};
void func(A* p)
{
B* p2 = p; // Error
}
Your code has several oddities.
You use private inheritance. In private inheritance you will not be able to convert to a derived class ever when you are not inside the class scope itself.
Even if you would inherit publicly, you will need at least one virtual function (and that should be the destructor) in the base class to use dynamic_cast.
Chances are you are doing something wrong when you need a lot of down-casts. You should probably rethink your design or usage of the provided API.
Typically, things would look like this:
class A {
public:
virtual ~A() {}
};
class Derived : public A {
};
void func(A* a) {
if(Derived* d = dynamic_cast<Derived*>(a)) {
// yeah, a is of type derived
} else {
// a is not of type Derived
}
}
is-a relationship is implemented by public inheritance. as you are inheriting it privately this is association relationship which is not is-a. so B is not an A. So you cannot store A in B or A cannot become an B
and dynamic_cast will not work as source type is not polymorphic
neither would static_cast work as ‘A’ is an inaccessible base of ‘B’
To make an is-a relationship you need to do class B: public A{}
For classes private inheritance is used by default.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I would like to get a vector/list of derived class pointers (one corresponding to each derived class from an abstract base class) given the base class name, for instance.
So to do exactly what you seems to want to do
#include <list>
class BaseObject { };
class Dinosaur : public BaseObject { };
class Hammer : public BaseObject { };
void
myFunction() {
std::list<BaseObject*> myList;
myList.push_back(new Dinosaur());
myList.push_back(new Hammer());
}
Note that the objects in the list won't free by themselves when the list will be destroyed. Either you have to do manually (Iterating over the list and calling delete), either look on something a bit complex if you are a C++ beginner, auto_ptr, and the magic world of the smart pointers ^^
You can't get a pointer to a class. A class is just a specification for an object. You only have something to point to once an object is created by instantiating the class.
If you have the following classes:
class FooBase {};
class DerivedFoo : public FooBase {};
class MoreDerivedFoo : public DerivedFoo {};
you can then create an object of type MoreDerivedFoo and point to it using a pointer of any of those types.
MoreDerivedFoo mdf = new MoreDerivedFoo();
FooBase* p1 = &mdf;
DerivedFoo* p2 = &mdf;
MoreDerivedFoo* p3 = &mdf;
Obviously each of these pointers will contain the same memory address.