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};
    };
};
7
  • 1
    How would you actually assign values to the data array? Commented Nov 10, 2012 at 0:52
  • 2
    Why did you put the word template at the beginning of your class definition? Commented Nov 10, 2012 at 0:53
  • 1
    @psyklopz, With C++11: bin_tree() : data{1, 2, 3, 4} {}. Commented Nov 10, 2012 at 0:59
  • 4
    @psyklopz: So you're just writing random keywords hoping a program comes out at the end? That is not the way to go. Read a C++ book and get yourself back up to speed. Commented Nov 10, 2012 at 1:14
  • 1
    Thanks, but I have no desire to learn C++. I have a small problem and a large deadline. Commented Nov 10, 2012 at 1:26

2 Answers 2

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";
}
Sign up to request clarification or add additional context in comments.

4 Comments

the bin_tree() : data{1, 2, 3, 4} { ... } syntax is valid only in a C++11 compliant compiler.
@NikB. I know this. Current C++ is C++11, read "Zoidberg'--" comment at stackoverflow.com/a/13223531/1762344
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.
In fact, I have added "C++11" in-code comment, before read your comment
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.

9 Comments

What the point of using std::array in that particular example (instead of old-style array) ?
@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.
"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?) ?
@EvgenyPanasyuk alright… so you don't like std::array. i'm not following your lead to where you want to take the discussion. chau.
@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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.