0

SockeClient.h file

#define SIZE_OF_BUFFER 4096 class SocketClient { public: SocketClient(int cfd); virtual ~SocketClient(); int recv(); int getFd(); protected: int m_fd; char *m_buffer; vector<char> m_vbuffer; }; 

I was trying to do

vector<char> m_vbuffer(SIZE_OF_BUFFER); 

And I got a syntax error... How do I initialize the vector with size of 4096. Thanks in advance

1
  • What is the error you got and where did you try to do this Commented Jun 21, 2011 at 8:29

3 Answers 3

3

Use member-initialization-list, in the constructor's definition as:

class SocketClient { public: SocketClient(int cfd) : m_vbuffer(SIZE_OF_BUFFER) { //^^^^^^^^^^^^^^^^^^^^^^^^^^^ member-initialization-list //other code... } protected: int m_fd; char *m_buffer; vector<char> m_vbuffer; }; 

You can use member-initialization-list to initialize many members as:

class A { std::string s; int a; int *p; A(int x) : s("somestring"), a(x), p(new int[100]) { //other code if any } ~A() { delete []p; //must deallocate the memory! } }; 
Sign up to request clarification or add additional context in comments.

1 Comment

@DumbCoder - reserve is not enough if you use the vector as a buffer. It needs to have a size() and not just a capacity().
1

Call m_vbuffer->reserve(SIZE_OF_BUFFER) in the constructor of SocketClient.

Comments

0

In addition to other answers consider using some circular buffer instead of vector. There is one in boost.

1 Comment

That would be overkill for allocating space for a vector with char elements.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.