1

I'm working at creating a class that is a binary tree. I would like to simplify this by creating a class that stores the data in an array, and then go back and make the magic happen.

However, the commented out line in the following section of code causes Visual Studio to spew ton of errors. It makes sense to me, but for some reason it won't compile. I'm not a C++ guy, but after a lot of Googling, nothing is working for me.


template 
class bin_tree
{
private:
    int *data;

public:
    int getData(int x)
    {
        return 1;
    };
    bin_tree() : data(new int[4])
    {
        //data = {1, 2, 3, 4};
    };
};
justin
  • 104,054
  • 14
  • 179
  • 226
psyklopz
  • 2,283
  • 4
  • 24
  • 29

2 Answers2

1

If size of your array is fixed (from your code, seems like it is the case), then you don't need to do dynamic allocation (i.e. "new").

http://coliru.stacked-crooked.com/a/97e112739e8a45de

class bin_tree
{
private:
    int data[4];

public:
    int getData(int)
    {
        return 1;
    };
    bin_tree()
        : data{1, 2, 3, 4} // C++11
    {
        // or:
        data[0] = 11;
        data[1] = 22;
        // ...
    };
};

#include <iostream>
int main()
{
    bin_tree tr;
    std::cout << "OK";
}
Evgeny Panasyuk
  • 9,076
  • 1
  • 33
  • 54
  • the `bin_tree() : data{1, 2, 3, 4} { ... }` syntax is valid *only* in a C++11 compliant compiler. – Nik Bougalis Nov 10 '12 at 00:58
  • @NikB. I know this. Current C++ is C++11, read "Zoidberg'--" comment at http://stackoverflow.com/a/13223531/1762344 – Evgeny Panasyuk Nov 10 '12 at 01:00
  • 1
    You may, but psyklopz (who mentions not having used C++ for 5 years) may not. It's fine and dandy to show the C++11 syntax, but you should at least explain it (or make a note in the code you show). At least that's my take on it. – Nik Bougalis Nov 10 '12 at 01:03
  • In fact, I have added "C++11" in-code comment, before read your comment – Evgeny Panasyuk Nov 10 '12 at 01:07
1

Welcome to C++. Use std::array for a fixed size array:

#include <array>

class bin_tree {
private:
    std::array<int, 4>data;
public:    
    bin_tree() : data({1, 2, 3, 4}) {
    }
    ...
};

If you need dynamic resizing, then use std::vector instead.

justin
  • 104,054
  • 14
  • 179
  • 226
  • What the point of using std::array in that particular example (instead of old-style array) ? – Evgeny Panasyuk Nov 10 '12 at 01:04
  • @EvgenyPanasyuk because i am encouraging it as the default go-to fixed size array representation. esp. for somebody who's "not a C++ guy". it provides a clear interface, error detection, good semantics. it's also effectively weightless in many regards. i think the inverse question should be asked -- why use `new[]` or a c array (as in your answer) in this case? the OP's implementation is likely more complex than the implementation required to demonstrate the problem. – justin Nov 10 '12 at 01:17
  • "clear interface" - bloated with named member functions (though understandable reasons). "error detection" - yes, I think this is definitely advantage even for that particular case, I forgot about this opportunity. "good semantics" - what do you mean exactly(value-semantic with copy?) ? – Evgeny Panasyuk Nov 10 '12 at 01:24
  • @EvgenyPanasyuk alright… so you don't like `std::array`. i'm not following your lead to where you want to take the discussion. chau. – justin Nov 10 '12 at 01:39
  • No, you get it wrong, I like std::array. My initial question was about usage of it in that particular example... – Evgeny Panasyuk Nov 10 '12 at 02:01
  • 1
    @EvgenyPanasyuk: Why wouldn't you use it? It's not bloated, as you suggest. Those member functions cost nothing, where nothing = an insignificant micro-fraction of a second of compile time. The question really should be, is there any particular reason you should use an old-style array here? Does it offer anything (significant) that `std::array` does not? – Benjamin Lindley Nov 10 '12 at 02:45
  • @BenjaminLindley , under "bloated with named member functions" I meant that those named member functions can be implemented as non-member functions (which are prefered). Check opinions from Stepanov, Stroustrup and Alexandrescu: http://stackoverflow.com/a/13318512/1762344 – Evgeny Panasyuk Nov 10 '12 at 02:50
  • @EvgenyPanasyuk: Fair enough, but I don't see how that's an argument for using old-style arrays, which are the most misbehaved objects in the language. – Benjamin Lindley Nov 10 '12 at 02:57
  • @BenjaminLindley it is not argument for using old-style array, it was response for "clear interface" statement. – Evgeny Panasyuk Nov 10 '12 at 02:58