I want to know what is the use scope of this[i]. I know this is clearly an useless question but let say it for science. What are the behaviors allow by the standard for indexing this ? I didn't find information about it, cppreference.
Stupid example:
#include <iostream>
class foo {
public:
int value;
void bar(std::size_t size, int a) {
for (std::size_t i = 0; i < size; i++) {
this[i].value = a--;
}
}
};
int main() {
foo foos[42];
(*foos).bar(sizeof foos / sizeof *foos, 42);
for (auto &foo : foos) {
std::cout << foo.value << std::endl;
}
return 0;
}
Is it only allow with array ? Maybe. What about heritage ? I think it's undefined behavior. Do you know some use-case ? It's just some questions about this[i], don't answer to these directly if you don't want to.
this is just a pointer. The ptr[n] syntax is identical to *(ptr + n).
Do you know some use-case ?
No, there's no valid reason to use this[n]. The class itself shouldn't assume that the current instance is part of an array or that there are other instances close by in contiguous memory.
Design the class in such a way that it will work independently of where/how it is instantiated.
Your code can be modernized/improved significantly.
Use std::array instead of C-style arrays.
Don't do manual pointer arithmetic.
Have foo only deal with the current instance.
Do not use endl unless you want to flush the cout buffer. Use \n instead.
class foo {
public:
int value;
void bar(int a) {
this->value -= a;
}
};
int main() {
std::array<foo, 42> foos;
for (auto &foo : foos) {
foo.bar(1);
std::cout << foo.value << '\n';
}
return 0;
}
Related
I have a struct Foo;
typedef struct {
int bar;
char baz;
} Foo;
Suppose I then declare an array of Foo as;
Foo* arr = new Foo[300];
And proceed to initialize every member with a loop. I would like very much to be able to get an array of all members bar;
int* barr_arr = ...
What is the most efficient way to do this? Is there some way to exploit the memory layout such that I need not loop over the entire Foo array?
Since we know the memory layout in advance could we exploit the fact that we know the address of every member if we're clever about alignment?
What is the most efficient way to do this? Is there some way to exploit the memory layout such that I need not loop over the entire Foo array?
I don't think there is away to do that without looping. You can simplify your code by using std::transform but std::transform does loop.
Also, I would recommend using std::vector instead of allocating an array using new.
std::vector<Foo> arr(300);
....
std::vector<int> bArr(arr.size());
std::transform(arr.begin(), arr.end(), bArr.begin(), [] -> (Foo const& f) { return f.bar; });
When you are initializing the first array, you can grab a pointer to the field inside each element and store that in a separate array.
struct Foo
{
int bar;
float baz;
};
const int SIZE = 5;
Foo foos[SIZE];
int *bars[SIZE];
for(int c = 0; c < SIZE; c++) {
foos[c].bar = c;
foos[c].baz = c;
bars[c] = &foos[c].bar; // Grab pointer to field
}
for(int c = 0; c < SIZE; c++) {
std::cout << "Bar Value: " << *bars[c] << std::endl;
}
If Foos typically exist in arrays, and corresponding arrays of bars and bazs often need to be accessed, I would suggest redesigning your data structures to better suit your problem. Obviously, we're not reading the code that inspired this question, but given the information provided, I might suggest something like:
struct FooArray {
int* bars;
char* bazes;
size_t n_elements;
};
This removes the need to allocate a new buffer for the bar array, which, depending on how many Foos are being processed, might entail significant memory savings.
I would also note that, if you're not working at a low level and don't actually need an int* but can do with a std::vector<int>, then #R Sahu's answer is likely a more appropriate solution.
The goal drives the design.
If your main use is to pass all bar members in a row, same for baz members, then create separate containers:
std::vector<int> bar;
std::vector<char> baz;
Then passing bar as an array is straightforward: just use bar.data().
If you add a constructor to your Foo that takes the size of array, you could have only one object of Foo. You can then make it that you can access either the whole vector data or individual elements with subscript:
#include <iostream>
#include <vector>
#include <memory>
struct Foo
{
std::vector<int> bars;
std::vector<char> bazs;
std::size_t size;
Foo(size_t size, int bar = 0, char baz = 0) :
bars(size, bar), bazs(size, baz), size{size}
{
}
auto operator[](size_t n)
{
// if (n >= size) ...
struct
{
int &bar;
char &baz;
} temp{ bars[n], bazs[n] };
return temp;
}
};
int main()
{
Foo arr(30, 100, 'a'); // 30 items
std::cout << arr[29].bar << std::endl;
std::cout << arr[29].baz << std::endl;
std::cout << arr.bars[29] << std::endl;
std::cout << arr.bazs[29] << std::endl;
std::unique_ptr<Foo> arr2 = std::make_unique<Foo>(25, 10, 'b'); // 25 items
std::cout << arr2->operator[](15).bar << std::endl;
std::cout << arr2->operator[](15).baz << std::endl;
arr2->bars[15] = 11;
std::cout << arr2->bars[15] << std::endl;
arr2->bazs[15] = 'c';
std::cout << arr2->bazs[15] << std::endl;
return 0;
}
Demo: https://ideone.com/TiVwOT
100
a
100
a
10
b
11
c
I needed a vector of a base class, and everywhere I looked the solution was to store it as a pointer. However, storing pointers makes things harder to use because of the lack of value semantics.
struct base
{
int a = 10;
};
struct derived : public base
{
int b = 5;
};
int main()
{
std::vector<std::reference_wrapper<base>> vec;
vec.push_back(*(new derived));
auto elem = static_cast<derived*>(&vec.at(0).get());
std::cout << elem->b << std::endl; // prints 5
return 0;
}
Is there any downside to just storing them as an std::reference_wrapper?
I have an array of pointers declared as a class member like this:
class Bar
{
private:
static constexpr int SIZE = 10;
Foo* m[SIZE];
}
In one of my class methods, I would like to return a pointer (or preferably, a reference) to this array. The array has a known size at compile time, but I am keeping track of how many items I have put in there (it is a buffer of stuff).
What is the best way to return a reference to this array in C++11 ?
Here are the things I have tried:
GetArray(Foo* &f[], unsigned &size) const
I like the syntax because it makes it clear that the reference value is an array of pointers, but this gives a compiler error: Declared as array of references of type Foo*
GetArray(Foo** &f, unsigned &size) const
{
f = m;
size = mSize;
}
Gives me: Error: assigning to Foo **' from incompatible type Foo *const[10]. Casting mFoo to (Foo**) alleviates the error, but IMHO, this is not elegant.
Nobody posted an answer using std::array yet, it is a very simple replacement:
class Bar
{
std::array<Foo *, 10> m;
public:
std::array<Foo *, 10> & getArray() { return m; }
std::array<Foo *, 10> const & getArray() const { return m; }
};
This seems to me a lot simpler than the hoops you have to jump through to use your C-style array version.
To avoid code duplication you could typedef std::array<Foo *, 10> FooArray; .
The technique of having both a const and a non-const implementation is a common pattern for accessor functions which return a reference or a pointer. (It's not required if your accessor returns by value, of course).
I would seek to use a std::array or a std::vector in most cases. If you are determined to use a raw array then you could go this way with it:
typedef int Foo;
typedef Foo* (&FooPtrArrayRef)[10]; // to make the syntax less hairy
class Bar
{
private:
Foo* m[10];
public:
// First way without using typedef
Foo* (&getArray())[10]
{
return m;
}
// Nicer looking way with a typedef
FooPtrArrayRef getArrayByRef()
{
return m;
}
};
int main()
{
Bar b;
Foo* (&array)[10] = b.getArray();
std::cout << (sizeof(array) / sizeof(Foo*)) << '\n';
// Alternative using "size deduction"
Foo* (&array2)[sizeof(b.getArray()) / sizeof(Foo*)] = b.getArray();
std::cout << (sizeof(array2) / sizeof(Foo*)) << '\n';
// MUCH nicer using the typedef
FooPtrArrayRef array3 = b.getArrayByRef();
std::cout << (sizeof(array3) / sizeof(Foo*)) << '\n';
}
The syntax is pretty obscure though.
The benefits of this is approach is that it retains the full type information of the array you are passing by reference. The obscure syntax is necessary to avoid the type collapsing to a Foo**. By retaining the full type of the array you retain the ability to know its size at compile time using the sizeof() operator.
here's another approach that returns the array reference and the current size as a tuple:
#include <tuple>
#include <functional>
#include <algorithm>
#include <iterator>
#include <iostream>
struct Foo {};
using FooBuffer = Foo*[10];
class Bar
{
public:
Bar()
: _m { nullptr }
{
_m[0] = new Foo;
_m[1] = new Foo;
_items = 2;
}
~Bar() {
for(auto fp : _m)
delete fp;
}
std::tuple<FooBuffer&, size_t> getInfo() {
return std::make_tuple(std::ref(_m), _items);
}
private:
Foo* _m[10];
size_t _items;
};
int main() {
Bar b;
auto info = b.getInfo();
FooBuffer& buf = std::get<0>(info);
size_t items = std::get<1>(info);
for(Foo** p = buf ; p != buf + items ; ++p) {
std::cout << "Foo at " << std::hex << *p << std::endl;
}
return 0;
}
I have to use some legacy code expecting a function pointer, let's say:
void LEGACY_CODE(int(*)(int))
{
//...
}
However the functionality I have is within a functor:
struct X
{
Y member;
X(Y y) : member(y)
{}
int operator()(int)
{
//...
}
};
How should I modify/wrap class X so that LEGACY_CODE can access the functionality within X::operator()(int) ?
Your question makes no sense. Whose operator do you want to call?
X a, b, c;
LEGACY_CODE(???); // what -- a(), b(), or c()?
So, in short, you cannot. The member function X::operator() is not a property of the class alone, but rather it is tied to an object instance of type X.
Search this site for "member function" and "callback" to get an idea of the spectrum of possible approaches for related problems.
The crudest, and quite possibly not-safe-for-use, workaround to providing a free function would go like this:
X * current_X; // ugh, a global
int dispatch(int n) { current_X->operator()(n); }
int main()
{
X a;
current_X = &a;
LEGACY_CODE(dispatch);
}
You can see where this is going...
A simple wrapper function looks like:
int wrapperfunction(int i) {
Functor f(params);
return f(i);
}
If you want to be able to pass the parameters to the functor itself, the simplest way is to sneak them in using (brr) a global variable:
Functor functorForWrapperfunction;
int wrapperfunction(int i) {
functorForWrapperfunction(i);
}
// ...
void clientCode() {
functorForWrapperfunction = Functor(a,b,c);
legacyCode(wrapperfunction);
}
You can wrap it with a class with a static method and a static member if you want.
Here's one compile-time solution. Depending on what you need, this might be a too limited solution for you.
template<typename Func, int Param>
int wrapper(int i)
{
static Func f(Param);
return f(i);
}
A thread-safe version under the restriction that the legacy code is not called with different parameters in a thread.
IMHO, one cannot get rid of global storage.
#include <boost/thread.hpp>
#include <boost/thread/tss.hpp>
class AA
{
public:
AA (int i) : i_(i) {}
void operator()(int j) const {
static boost::mutex m; // do not garble output
boost::mutex::scoped_lock lock(m);
std::cout << " got " << j << " on thread " << i_ << std::endl;
Sleep(200); }
int i_;
};
// LEGACY
void legacy_code(void (*f)(int), int i) { (*f)(i); }
// needs some global storage through
boost::thread_specific_ptr<AA> global_ptr;
void func_of_thread(int j)
{
AA *a = global_ptr.get();
a->operator()(j);
}
void worker(int i)
{
global_ptr.reset(new AA(i));
for (int j=0; j<10; j++)
legacy_code(func_of_thread,j);
}
int main()
{
boost::thread worker1(worker,1) , worker2(worker,2);
worker1.join(); worker2.join();
return 0;
}
I am puzzled by some issues when trying to design interacting classes.
If a class A needs some data from class B, either in the form of a pointer or standard container format, (or even lets become more ambitious and say in the form of some shared_ptr members to stl containers.), my way is to use a function that returns the same kind of arguments and design class B methods with conforming arguments? Are there a general rules of thumb for designing interacting classes and share data between these classes?
Could you please draw a general scheme for some common situations that are generally encountered in practice(and along some examples are appreciated)? I guess I should read some examples on class interaction in C++, any pointers on this are also appreciated?
A small sample could be:
#include <iostream>
#include <vector>
#include <iterator>
#include <cassert>
using namespace std;
class A{
public:
A(int s, int val):sz(s), val(val), v(new vector<int>){}
int fill_vector(){
for(int k=0;k!=sz;++k)
v->push_back(val);
return 0;
}
~A(){
cout << "Dtor is deleting the pointer to vector\n";
delete v;
}
vector<int>* get_pointer_to_vector_in_A(){ return v; }
private:
int sz, val;
vector<int> *v;
};
class B{
public:
B(){} // does nothing basically
int print_vector_contents_from_A( vector<int> *v_from_A ) const
{
assert(!v_from_A->empty());
copy(v_from_A->begin(), v_from_A->end(),
ostream_iterator<int>(cout, "\n") );
}
};
int main()
{
A a(10, 4);
a.fill_vector();
B b;
b.print_vector_contents_from_A( a.get_pointer_to_vector_in_A() );
return 0;
}
It rather depends on what A is, conceptually. If A can be validly viewed as a sequence of int, then I'd implement size_t size() const and int &operator[](size_t) (+ its const counterpart) on it. These can just delegate their activities to v.size and v[] or v.at.
In B, you can then define
static void B::print_contents(A const &a)
{
for (size_t i=0; i < a.size(); i++)
std::cout << a[i] << '\n';
}
A member that returns an std::vector<int>* breaks encapsulation: you can never change the implementation of A away from std::vector<int> except with very ugly hacks to ensure get_pointer_to_vector_in_A still works with the same semantics.
One great one-directional way to do this is:
class A {
public:
void fill_vector();
int vec_size() const { return vec.size(); }
int get_data(int i) const { return vec[i]; }
};
class B {
public:
B(A &a) : a(a) { }
void init() { a.fill_vector(); }
void fetch_and_print()
{ for(int i=0;i<a.vec_size();i++) std::cout << a.get_data(i); }
private:
A &a;
};
int main() {
A a;
B b(a);
b.init();
b.fetch_and_print();
}
The constructor parameter for class B is the important bit.