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.
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 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);
}
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.
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 10 years ago.
I had used an abstract base class for interface and derived class for implementation. See the code below.. Can it be associated with any standard design patterns in c++?
Class people
{
public:
virtual void setname(string name)=0;
virtual void SetAge(int Age)=0;
//etc consists of only pure virtual functions like above
};
Class Students: public people
{
void setname(string name)
{
//implementation of the function
}
void SetAge(int Age) { //implementation }
}
And i had defined many classes as above and objects are created in constructor of a Buildclass as:
Buildclass::Buildclass()
{
people *Obj = (people*) new Students();
interface *obj1 = (interface*) new implementation();
}
And i had provided getinstance functions for above to be used in another layer
void BuildClass::getPeopleinstance()
{
return Obj;
}
void BuildClass::getAnotherinstance()
{
return Obj1;
}
Can the above code be associated to any design pattern? Please let me know? I am unable to find out.
You appear to be using the Factory pattern, but it really doesn't matter. Focus on writing good code, not design patterns.
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=)