accessing a pointer member variable in derived class [closed] - c++

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.

Related

move constructor: how to handle container attribute? [closed]

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.

How can I pass to a method an argument that could be a function of any type? [closed]

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 to make a library as an assignment that would implement a menu-like functionality on a linux terminal. So I have to for each submenu enable a position to be either another submenu or a call to some function.
I was wondering how to implement such a call. Since it is to be a library, it could be used to a dozen of different kinds of functions, taking different number and types of arguments.
How to implement such a method calling another functions that would not assume anything about those functions any yet could call them? Should I use templates somehow or is there another method for that; or maybe I should implement it in a whole other way?
You can use objects with virtual methods to do this. Then the application can decide, which parameters to put into the callback object.
class MenuItem;
// Callback Interface
struct MenuCallback
{
virtual void activated(MenuItem* sender) = 0;
virtual ~MenuCallback(){}
};
// Application Code
struct ConcretMenuCallback: MenuCallback
{
ConcretMenuCallback(int parameter1);
void activated(MenuItem* sender)
{
// do something with the data stored in this object
}
};
// Example Item
class MenuItem
{
MenuItem(MenuItem*parent, std::string caption, MenuCallback* cb);
void notify();
}
MenuItem::notify()
{
// call the callback-Object
if(m_callback)
m_callback->activated(this);
}

C++ method calls from the same class [closed]

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.

Is there any way to pass a variable of class in other class? [closed]

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 want two classes. I have a static variable in class Class1 and I want to pass the value that it got to class Class2.
For Example :
//Class1.h
{
static int x;
int Method1();
}
//Class1.cpp
{
int Class1::x=0;
int Class1::Method1(){
x=2;
}
}
Now Class2
//Class2.cpp
{
Class1 cls;
cout<<cls.x<<endl;//it shows 0 value
}
I assume x is public:
#include "class1.h"
int xVal = Class1::x;
You need to declare the other class you want to access the variable from as "friend"
class Class1 {
friend class Class2;
// ...
}
Now you can access all variables from Class1 in Class2.

how to over come the issue of object slicing in c++ [closed]

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=)