I've tried to SSCE my problem as best possible, but it involves mulitple objects defined in C++. They are simple, though - I think its best if I share my code before explaining further:
#include <iostream>
#include <vector>
struct Cell {
bool visited;
Cell():visited(false) {}
void setVisited(bool val) {visited = val;}
bool beenVisited() {return visited;}
};
struct Vector2D
{
int size;
std::vector<Cell> myVector;
Vector2D(int n): size(n), myVector(n*n) {}
Cell& getAt(int x, int y) {return myVector[((x * size) +y)];}
};
int main()
{
Vector2D vec = Vector2D(1);
Cell cell= vec.getAt(0,0);
cell.setVisited(true);
cell = vec.getAt(0,0);
if (cell.beenVisited() == false)
std::cout << "Why is this not true like I set it a moment ago?\n";
}
I apologize sincerely for all of this, but it is needed to make the point. As you can see, I getAt() what I think to be Cell object, set its visited instance data to true, and then switch off to another cell. Why, then, when i come back to that same cell, find that the visited value is false instead of true?! It's like it isn't registering my private data change!
What is the best way to do this?
Thanks
Cell cell= vec.getAt(0,1);
copy of object.
Use
Cell& cell = vec.getAt(0, 1);
or simply
vec.getAt(0, 1).setVisited(true);
EDIT.
This code should works.
using namespace bob;
Vector2D vec = Vector2D(5);
vec.setAt(0,0, Cell(0,0));
vec.setAt(0,1, Cell(0,1));
vec.setAt(0,2, Cell(0,2));
Cell& cell= vec.getAt(0,1);
cell.setVisited(true);
Cell cell1 = vec.getAt(0,2);
cell1 = vec.getAt(0,1);
if (cell1.beenVisited() == false)
{
std::cout << "Why is this not true like I set it a moment ago?" << std::endl;
}
http://liveworkspace.org/code/53634eda052a07885d4e6c062a0fd302
ForEveR's answer is correct -- you need to store the value returned by getAt() in a reference variable, rather than copying it into a value variable.
You might consider explicitly declaring that the "Cell" class should never be copied, which will help you catch this type of error sooner. This can be done by declaring a private copy constructor (with no body); or if you're using boost, then it can be done by inheriting from the base class "boost::noncopyable" (docs for noncopyable).
Related
This is certainly related to a bunch of other questions which have been answered, but I have been unable to derive the answer for my specific case from them, largely because I'm not actually a programmer; I'm just an engineer who happens to have to write some code.
Here's the situation:
I have a bunch of variables I'd like to collect together, probably into a structure.
All but two I would like to initialize to zero; two specific variables (which don't happen to be the first two) need to be initialized to one.
The actual names are unique and meaningful enough that using a vector wouldn't be appropriate, plus there are some doubles in there too. I'm keeping my example below simple for clarity.
Because of the project I'm working on, I'm stuck with C++98, so even if C++11 has more elegant solutions, they won't work for me.
I am thinking something along these lines for the structure itself:
struct allFlags
{
int flagAA;
int flagAB;
int flagAC;
int flagAD;
int flagAE;
// ...
// there's about 100 variables total
// ...
int flagZZ;
};
I want to have all the flags initialized to 0 except for flagAD and flagAE, which should be 1.
So first of all, I am not sure if I should use typedef struct allFlags or struct allFlags. Next, I am not sure if I should be creating a constructor (which I think only would apply in the case of no typedef?) or making the defaults happens when I instantiate the structure. I have seen things like this (which would be put inside the struct definition):
allFlags() : flagAD(1), flagAE(1) { /*not sure of I'd need anything here*/ }
but I wouldn't want to have to list out all other ~98 variables by name individually in the constructor body to set them to zero. I have also seen things using memset which could potentially help, but I'm not sure the best way to do it.
And finally one additional related question is how to actually declare an instance of my structure (which results in the initial values I want). It looks like sometimes a struct is instantiated with the new keyword and sometimes is it treated more like a base data type, i.e. I have seen both of these in searching:
allFlags flagset1;
flagset2 = new allFlags;
I have also seen syntax which would be like this rather than using a constructor at all:
allFlags flagset3 = {}; // to zero all fields first
flagset3.flagAD = 1;
flagset3.flagAE = 1;
but I'd rather keep the instantiation as clean and simple as possible.
Please forgive the question. I have tried to do my homework before asking, but my C++ knowledge is mediocre at best and so some of the seemingly relevant answers I've found I either didn't fully understand or just raised more questions.
If you feel comfortable with using templates, you can use a class template to automate clean initialization of all member variables of allFlags.
// class template to help initialize members cleanly.
template <typename T>
struct flag
{
// Constructors
flag() : val(0) {}
flag(T const& v) : val(v) {}
// Automatic cast operators to T
operator T& () { return val; }
operator T const& () const { return val; }
// Comparison operators
bool operator==(flag const& rhs) const { return val == rhs.val; }
bool operator!=(flag const& rhs) const { return val != rhs.val; }
bool operator<(flag const& rhs) const { return val < rhs.val; }
T val;
};
typedef flag<int> IntFlag;
typedef flag<double> DoubleFlag;
struct allFlags
{
// Initialize all flags bug flagAD to zero.
allFlags() : flagAD(1) {}
IntFlag flagAA;
IntFlag flagAB;
IntFlag flagAC;
IntFlag flagAD;
IntFlag flagAE;
IntFlag flagZZ;
};
#include <iostream>
int main()
{
allFlags f;
std::cout << f.flagAA << " " << f.flagAD << std::endl;
}
Output:
0 1
You answered your own question quite well:
allFlags flagset3 = {}; // to zero all fields first
flagset3.flagAD = 1;
flagset3.flagAE = 1;
It is clean, and very clear about your intentions. Later, when someone else has to read your code they will understand exactly what you are trying to do.
It is similar to what you see in device driver programming:
registerX = 0 | 1 << BIT2 | 1 << BIT3;
I hope someone can help me. In an effort to be more specific about what I really need, and trim down my code, I have changed from having a vector purely of my Class, to having a vector of objects of a new class, of which my original class is a type within.
I hope I have explained myself clearly up until this point. I will show the relevant classes:
class screen_area
{
private:
int my_id, my_x, my_y, my_width, my_height;
bool active=true;
public:
screen_area (int button_id=0, int x=0, int y=0, int width=0, int height=0, bool isactive=true)
{
my_id = button_id;
my_x = x;
my_y = y;
my_width = width;
my_height = height;
active = isactive;
}
~screen_area()
{}
class bet
{
private:
int wager = 0;
int multiplier = 0;
public:
screen_area area;
bet(int wager, int multiplier, screen_area area)
{};
~bet()
{};
There is a little more to them, but this is the bread and butter. Now previously I had used a member function within "screenarea", to return any value I had wanted from a specific object:
int getvalue(int value)
{
switch(value)
{
case 1 :
return my_id;
case 2 :
return my_x;
case 3 :
return my_y;
case 4 :
return my_width;
case 5 :
return my_height;
case 6 :
return active;
}
}
And I have modified a lookup function to use this member function on the screenarea that is a type contained within "bet".
int returnbuttonid(int mousex, int mousey, std::vector<bet> *buttons)
{
for (auto ep : *buttons )
{
if ((ep.area.getvalue(2) > mousex) && (ep.area.getvalue(3) > mousey))
{int id_value = ep.area.getvalue(1);
return id_value;
}
}
}
However... it returns garbage. I'm clearly missing something, but I am going through it logically and it all seems to make sense.
Sorry in advance if it is something simple! And I appreciate that this may seem long winded but I would really appreciate some help!
And just to be super clear... this is how I am calling it:
vector<bet> localbuttons; //Declaration of Vector
load_map("data.dat", &localbuttons); //load buttonmap using function
int buttonpressed = returnbuttonid(100,300, &localbuttons);
In response to a very speedy comment. It's clear that the problem at least starts with an unpublished piece of code. My vector of "bet" is not being filled with the arguments I am passing to it when I try to overload the constructor. I presumed I had corrected the syntax correctly when I created the new class "bet", but after probing the vector it is not showing any data.
In my function load_map:
bool load_map(std::string path, std::vector<bet> *buttons)
{
//setup file
ifstream inputFile( path.c_str() );
//
//The stuff in the middle here is irrelevant
//and I've take it out to make this tidier
buttons->push_back(bet(0,0, screen_area(id,x,y,width,height, true)));
}
return 0;
}
Now the only part of this that has changed since I had this function was working is:
buttons->push_back(bet(0,0, screen_area(id,x,y,width,height, true)));
So I am guessing this is where the problem originates. The variables are not overloading the default screen_area constructor. So when I:
cout << localbuttons[1].area.my_id << endl;
I always see whatever value I place in the default constructor. It is "0" in the constructor I have posted here, but if I change it, it changes correspondingly.
And I shouldn't have said garbage, I was at fault for thinking I had correctly identified the area of the problem, and trying to be concise. So I guess I should be asking first... How can I correctly overload this "screenarea" constructor?
The problem here was in the Constructor of the Bet class.
After having a look here:
http://www.cplusplus.com/doc/tutorial/classes/
I rewrote the constructor in the Bet class:
bet(int w, int m, int button_id=0, int x=0, int y=0,
int width=0, int height=0, bool isactive=true)
: area(button_id, x, y, width, height, isactive),
wager(w), multiplier(m)
{};
My apologies if I wasted anyone time with misdirection, and thanks for the sensible advice from Jonathon Potter.
I'm not sure why I thought you could call constructors within parentheses. My compiler didn't seem to complain about it, but from what I can gather - I was just creating a temporary object.
I am working with a large code base, and there are a number of publicly defined variables. Unfortunately, the functions of accessing these variables has changed, and this new functionality would be best encapsulated by public accessors and a private instance variable.
So, I am trying to make this change. To do so, I planned to make each public property private and then create accessors. But, I don't want to change any of the code which accesses the old public properties. For example:
After changing the public property to private, I have the following class:
class Test {
private:
int item = 5;
public:
int GetItem() {
return item;
};
void SetItem(int new_item) {
item = new_item;
};
};
In the past, "item" used to be a public property of the class, and it was accessed through:
Test* t = new Test();
int item = t->item;
Now though, I need to add new functionality to the way in which "item" is retrieved. For example:
int GetItem() {
// Some complicated code which changes "item"
return item;
};
How can I keep the same syntax:
int item = t->item;
But have this actually perform:
int item = t->GetItem();
Any help is greatly appreciated!
You can make int item = t.item; work, by defining item as a member variable whose type is a helper class with a custom conversion operator int() defined. Also, operator=(int new_value) to intercept the set operation.
What you can't make work is
int& item = t.item;
or
int* pitem = &t.item;
because both of these enable direct memory access, without going through any getter or setter. When creating the reference or pointer, you can't even determine how many accesses there will be or whether they will be reads or writes.
C++ is a compiled non-reflective language, i.e. you can't just "look names up as you access an element", because in the binary, there are no names anymore.
So, no, what you want is impossible. (at least not without restrictions – see Ben Voigt's excellent answer; having a "transparent" property which is in fact a getter call surely isn't worth the pitfalls you're building with that-)
Also, please don't let your C++ become Java just for the sake of having getters and setters – if they don't actually add security, I don't really see the point of using them
In case that your question is based in the fact that you don't want to call 2 different functions for setting and getting, you can make a function that returns a reference of the member:
int& Item()
{
// Some complicated code which changes *items
return item;
}
as you can see, the return type is int& instead of int. so you can use this function this way
t.Item() = someValue;
To expand on Ben Voight's answer, you can define a proxy template that allows this without the boiler plate:
template <typename Return, typename Containing, Return (Containing::* func)()>
struct proxy
{
Containing& c;
proxy(Containing& c) : c(c) {}
operator Return() { return (c.*func)(); }
Return& operator=(const Return& r) { return (c.*set)() = r; }
};
Then to define a "property"
class c {
int y_;
int get_y() { std::cout << "Getting y" << std::endl; return y_; }
public:
proxy<int, x, &x::get_y> y;
c() : y(*this) {}
};
And in client code
int main() {
c val;
val.y = 5;
std::cout << val.y << std::endl;
}
I know that references can extend the lifetime of a return value in C++. With this phylosophy, I tried the following: I have three clases, 'tensor', 'view' and 'mutable_view'. Operator () on a tensor returns a "const view" object. This view has a private copy constructor so that the view cannot be copied, as it keeps information about the tensor that might not survive beyond the current statement.
#include <iostream>
#include <algorithm>
struct tensor {
int data[10];
class view {
const int *const data;
view();
view(const view &);
public:
view(const int *new_data) : data(new_data) {}
int operator*() const { return *data; }
};
class mutable_view {
int *const data;
mutable_view();
mutable_view(const mutable_view &);
public:
mutable_view(int *new_data) : data(new_data) {}
void operator=(const view &v) {
*data = *v;
}
};
tensor(int n) {
std::fill(data, data+10, n);
}
const view operator()(int ndx) const {
return view(data + ndx);
}
mutable_view at(int ndx) {
return mutable_view(data + ndx);
}
};
int main()
{
tensor a(1);
tensor b(2);
b.at(2) = a(2);
for (int i = 0; i < 10; i++)
std::cout << "a[i] = " << b.data[i] << std::endl;
for (int i = 0; i < 10; i++)
std::cout << "b[i] = " << b.data[i] << std::endl;
exit(0);
}
The problem is that, while this code works in gcc (depends on the version), icc signals a warning and open64 simply does not build it: it demands that the constructors from 'view' be public. Reading icc's message the idea seems to be that the right hand value could be potentially copied by the compiler and thus constructors are needed.
Is this really true? Is there a workaround that preserves the syntax I want to build? By the way they are built, and in order to avoid inefficient implementations based on shared_ptr or other stuff, I need to keep the 'view' objects un-copiable.
Edit 1:
tensor cannot control the lifetime of the views. The views are created by the accessors and their lifetime is limited to the statement where they are used, for the following reasons:
These views are only used for two things: (i) copying data, (ii) extracting portions of the tensor.
The tensors are multidimensional arrays that implement copy-on-write semantics, which means that the views cannot be long-lived objects: if the data changes, they expire.
Edit 2:
Changed the pseudocode description (guys, if you see '...' do you expect it to be compilable?) with one that builds on 'icc' and does not on clang/open64
Go ahead and let the default copy constructors be public. And document that a view or mutable_view is "invalidated" when its tensor is changed or destroyed.
This parallels how the Standard Library deals with iterators, pointers, and references that have a lifetime which depends on another object.
As others already pointed out you missed () here:
const view operator(int ndx) const;
Anyway this declaration means that return value is copied. If you want to avoid copying just return reference for an object:
const view& operator()(int ndx) const;
As I understand 'tensor' is container of 'views' so it manages there lifetime and its safe to return reference. For the same reason tensor::at should return reference to mutable_view:
mutable_view& at(int ndx);
Another question is about default constructor of 'view' - it looks like 'tensor' has to be a friend of 'view' to be able to create its instances
By the way - prefer using 'size_t' as index type instead of just 'int'
My overall feeling of this code - you are trying to implement kind of domain language. Maybe it's better to focus on concrete calculation task?
Consider the following class member:
std::vector<sim_mob::Lane *> IncomingLanes_;
the above container shall store the pointer to some if my Lane objects. I don't want the subroutins using this variable as argument, to be able to modify Lane objects.
At the same time, I don't know where to put 'const' keyword that does not stop me from populating the container.
could you please help me with this?
thank you and regards
vahid
Edit:
Based on the answers i got so far(Many Thanks to them all) Suppose this sample:
#include <vector>
#include<iostream>
using namespace std;
class Lane
{
private:
int a;
public:
Lane(int h):a(h){}
void setA(int a_)
{
a=a_;
}
void printLane()
{
std::cout << a << std::endl;
}
};
class B
{
public:
vector< Lane const *> IncomingLanes;
void addLane(Lane *l)
{
IncomingLanes.push_back(l);
}
};
int main()
{
Lane l1(1);
Lane l2(2);
B b;
b.addLane(&l1);
b.addLane(&l2);
b.IncomingLanes.at(1)->printLane();
b.IncomingLanes.at(1)->setA(12);
return 1;
}
What I meant was:
b.IncomingLanes.at(1)->printLane()
should work on IncomingLanes with no problem AND
b.IncomingLanes.at(1)->setA(12)
should not be allowed.(In th above example none of the two mentioned methods work!)
Beside solving the problem, I am loking for good programming practice also. So if you think there is a solution to the above problem but in a bad way, plase let us all know.
Thaks agian
A detour first: Use a smart pointer such shared_ptr and not raw pointers within your container. This would make your life a lot easy down the line.
Typically, what you are looking for is called design-const i.e. functions which do not modify their arguments. This, you achieve, by passing arguments via const-reference. Also, if it is a member function make the function const (i.e. this becomes const within the scope of this function and thus you cannot use this to write to the members).
Without knowing more about your class it would be difficult to advise you to use a container of const-references to lanes. That would make inserting lane objects difficult -- a one-time affair, possible only via initializer lists in the ctor(s).
A few must reads:
The whole of FAQ 18
Sutter on const-correctness
Edit: code sample:
#include <vector>
#include <iostream>
//using namespace std; I'd rather type the 5 characters
// This is almost redundant under the current circumstance
#include <vector>
#include <iostream>
#include <memory>
//using namespace std; I'd rather type the 5 characters
// This is almost redundant under the current circumstance
class Lane
{
private:
int a;
public:
Lane(int h):a(h){}
void setA(int a_) // do you need this?
{
a=a_;
}
void printLane() const // design-const
{
std::cout << a << std::endl;
}
};
class B
{
// be consistent with namespace qualification
std::vector< Lane const * > IncomingLanes; // don't expose impl. details
public:
void addLane(Lane const& l) // who's responsible for freeing `l'?
{
IncomingLanes.push_back(&l); // would change
}
void printLane(size_t index) const
{
#ifdef _DEBUG
IncomingLanes.at( index )->printLane();
#else
IncomingLanes[ index ]->printLane();
#endif
}
};
int main()
{
Lane l1(1);
Lane l2(2);
B b;
b.addLane(l1);
b.addLane(l2);
//b.IncomingLanes.at(1)->printLane(); // this is bad
//b.IncomingLanes.at(1)->setA(12); // this is bad
b.printLane(1);
return 1;
}
Also, as Matthieu M. suggested:
shared ownership is more complicated because it becomes difficult to
tell who really owns the object and when it will be released (and
that's on top of the performance overhead). So unique_ptr should be
the default choice, and shared_ptr a last resort.
Note that unique_ptrs may require you to move them using std::move. I am updating the example to use pointer to const Lane (a simpler interface to get started with).
You can do it this way:
std::vector<const sim_mob::Lane *> IncomingLanes_;
Or this way:
std::vector<sim_mob::Lane const *> IncomingLanes_;
In C/C++, const typename * and typename const * are identical in meaning.
Updated to address updated question:
If really all you need to do is
b.IncomingLanes.at(1)->printLane()
then you just have to declare printLane like this:
void printLane() const // Tell compiler that printLane doesn't change this
{
std::cout << a << std::endl;
}
I suspect that you want the object to be able to modify the elements (i.e., you don't want the elements to truly be const). Instead, you want nonmember functions to only get read-only access to the std::vector (i.e., you want to prohibit changes from outside the object).
As such, I wouldn't put const anywhere on IncomingLanes_. Instead, I would expose IncomingLanes_ as a pair of std::vector<sim_mob::Lane *>::const_iterators (through methods called something like GetIncomingLanesBegin() and GetIncomingLanesEnd()).
you may declare it like:
std::vector<const sim_mob::Lane *> IncomingLanes_;
you will be able to add, or remove item from array, but you want be able to change item see bellow
IncomingLanes_.push_back(someLine); // Ok
IncomingLanes_[0] = someLine; //error
IncomingLanes_[0]->some_meber = someting; //error
IncomingLanes_.erase(IncomingLanes_.end()); //OK
IncomingLanes_[0]->nonConstMethod(); //error
If you don't want other routines to modify IncomingLanes, but you do want to be able to modify it yourself, just use const in the function declarations that you call.
Or if you don't have control over the functions, when they're external, don't give them access to IncomingLanes directly. Make IncomingLanes private and provide a const getter for it.
I don't think what you want is possible without making the pointers stored in the vector const as well.
const std::vector<sim_mob::Lane*> // means the vector is const, not the pointer within it
std::vector<const sim_mob::Lane*> // means no one can modify the data pointed at.
At best, the second version does what you want but you will have this construct throughout your code where ever you do want to modify the data:
const_cast<sim_mob::Lane*>(theVector[i])->non_const_method();
Have you considered a different class hierarchy where sim_mob::Lane's public interface is const and sim_mob::Really_Lane contains the non-const interfaces. Then users of the vector cannot be sure a "Lane" object is "real" without using dynamic_cast?
Before we get to const goodness, you should first use encapsulation.
Do not expose the vector to the external world, and it will become much easier.
A weak (*) encapsulation here is sufficient:
class B {
public:
std::vector<Lane> const& getIncomingLanes() const { return incomingLanes; }
void addLane(Lane l) { incomlingLanes.push_back(l); }
private:
std::vector<Lane> incomingLanes;
};
The above is simplissime, and yet achieves the goal:
clients of the class cannot modify the vector itself
clients of the class cannot modify the vector content (Lane instances)
and of course, the class can access the vector content fully and modify it at will.
Your new main routine becomes:
int main()
{
Lane l1(1);
Lane l2(2);
B b;
b.addLane(l1);
b.addLane(l2);
b.getIncomingLanes().at(1).printLane();
b.getIncomingLanes().at(1).setA(12); // expected-error\
// { passing ‘const Lane’ as ‘this’ argument of
// ‘void Lane::setA(int)’ discards qualifiers }
return 1;
}
(*) This is weak in the sense that even though the attribute itself is not exposed, because we give a reference to it to the external world in practice clients are not really shielded.