C++ Inheritance of abstract reference members for polymorphism [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have an abstract class with N protected members:
class Something {
protected:
UINT someVal;
std::vector<SomeType> g_MyVec;
// some virtual abstract methods ...
public:
UINT getSomeVal() { return someVal; }
std::vector<SomeType> GetVec() { return g_MyVec; }
}
class SubClass : public Something {
public:
SubClass() { // no members in this class, all inherited from super
someVal = 5; // this sticks
g_myVec = { .. correct initialization }; // this doesn't stick
}
}
The client of this code does:
Something* s = &SubClass();
s->getSomeVal(); // OK, has 5 in it.
s->GetVec(); // Nada, it returns 0 size, nothing at all... WHY?!
Enlightenment is much appreciated.

You are taking an address of the temporary. It's a UB and incorrect code. Subclass gets destroyed along with the vector after the ;
Correct way to do this would be (Assuming no C++11):
Something* s = new Subclass();
s->getSomeVal(); // OK, has 5 in it.
s->GetVec();
delete s;

Related

using a shared_ptr object in constructor works but not in destructor [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I have a class where the construction is like:
class CLSS
{
public:
CLSS(const std::shared_ptr<someType>& pobj)
{
std::shared_ptr<someType> obj = pobj;
obj->somefunc("DDDD")
}
~CLSS()
{
}
};
which works with now problem. However when I put the same function of obj->info("DDDD") in the diconstructor, it returns error, that is:
...
~CLSS()
{
obj->info("DDDD")
}
....
---------------edit
I tried
class CLSS
{
public:
std::shared_ptr<someType> obj;
CLSS(const std::shared_ptr<someType>& pobj)
{
obj = pobj;
obj->somefunc("DDDD")
}
~CLSS()
{
}
};
but still does not compile, the errors are not very readble.
obj is a local variable in the constructor. It is destroyed when the constructor ends.
You need to declare it as a member of your class.

when we call function, is argument parenthesis () is needed if no argument pass like func; vs func(); [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
The static function getROI always returns 1 and also causes a warning :
the address of 'static float Account::getROI()' will never be NULL
#include <iostream>
using namespace std;
class Account
{
private:
int account_no;
int balance;
static float ROI;
public:
void setBalance(int x){balance=x;}
int getBalance(){return balance;}
void setAccountNo(int y){account_no=y;}
int getAccountNo(){return account_no;}
static void setROI(float z){ROI=z;}
static float getROI(){return ROI;}
};
float Account::ROI =0;
int main()
{
cout << "STATIC" << endl;
Account obj;
obj.setAccountNo(3435647);
obj.setBalance(1000000);
obj.setROI(4.9);
cout<<"Account No : "<<obj.getAccountNo()<<endl;
cout<<"Balance = "<<obj.getBalance()<<endl;
cout<<"Rate of int= "<<obj.getROI;
return 0;
}
You want obj.getROI(), not obj.getROI
It says the address will never be zero.
Which is true ... if the address were zero, reading it would be de-referencing a null pointer.
So you think you're looking at the data, but you're really looking at a pointer to the data, and (I presume) comparing that address to zero.

Friend function Random behaviour [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
#include<iostream>
class FooA
{
private :
friend class FooB;
void Hello();
void Hello2();
private:
void Hello3();
int m_iData;
};
class FooB
{
void fun()
{
FooA objA;
objA.Hello(); // right
objA.Hello2(); // right
objA.Hello3(); // right
//ojbA.m_iData = 0; // compile error
}
};
usually if we access Data member Function or Member variable which is private by an object directly throws error.
but in this scenario how it is able to access Hello(),Hello2(),Hello3() Functions and why it's throwing error in accessing m_iData.
The b and the j in objA have been swapped to form the unknown identifier ojbA.
Change ojbA.m_iData = 0; to objA.m_iData = 0;

Referring to static member of a class result in "not a static member of class" [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I want to create a class were classes are derived from when they use a certain feature of the program that has to be initialized only once.
I created the class with a static bool member, and a default constructor. The default constructor checks the static member and runs the initialization if needed:
class WireUser {
public:
static bool wireIntialized;
WireUser::WireUser();
};
bool WireUser::wireInitialized = false;
WireUser::WireUser() {
if (!wireInitialized) {
Wire.begin();
wireInitialized = true;
}
};
When i try to compile this i get the following error:
error: 'bool WireUser::wireInitialized' is not a static member of 'class WireUser'
bool WireUser::wireInitialized = false;
If i remove the line were the member is initialized as false, the same errors occurs on the line containing if (!wireInitialized) {
I don't understand what i'm doing wrong.
There's a number of typos and small errors/warnings in your code (steps for fixing explained in the comments):
class WireUser {
public:
static bool wireInitialized;
WireUser(); // Don't use scope specifiers in declarations: WireUser::WireUser();
};
bool WireUser::wireInitialized = false; // Typo fixed here 'wireInitialized' isn't the
// same as 'wireIntialized'
WireUser::WireUser() {
if (!wireInitialized) {
// Wire.begin(); Commented out because it's not in context of your question
wireInitialized = true;
}
} //; <<<< Omit the semicolon to get rid of the warning
int main (){
WireUser wu;
return 0;
}
Live Demo.
Also you should consider to make that thread safe for initialization.
A std::atomic<bool> might be the right choice:
#include <atomic>
class WireUser {
public:
static std::atomic<bool> wireInitialized;
WireUser();
};
std::atomic<bool> WireUser::wireInitialized = {false};
WireUser::WireUser() {
if (!wireInitialized.exchange(true)) {
// Wire.begin();
// wireInitialized = true;
}
}
int main (){
WireUser wu;
return 0;
}
Live Demo

Undefined Reference to Function error? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have seen other similar questions being asked but I could not figure out what the problem is. I have a declaration in an inventory class as:
class Inventory
{
Public:
void print();
void sell(Item*);
void add();
void find(string);
Private:
Item* first;
}
And then in the inventory.cpp I have:
void sell(Item* item_name)
{
..........................
}
And the error comes from calling it in main() as:
Inventory store_inventory;
Item* cur_item;
cout<<"Item name: ";
string name;
cin>>name;
cur_item = find(name); //find returns Item*
store_inventory.sell(cur_item);
The error is one the line for the call to sell. Any ideas?
The definitions need to indicate that it is a member function of Inventory:
void Inventory::sell( Item* item_name )
{
// ...
}
Also, there are issues with the find() function. You declare it as a void member function, but use at as if it is a free function that returns an Item*.
Is Item a class?
You should change the void type to the type you want to be returned item.
Also whem you call your sell function, at the parameter if you want to pass the same pointer, you must use the & before the variable name like sell(item* &varname).