Reputation: 3013
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
Upvotes: 0
Views: 439
Reputation: 361252
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!
}
};
Upvotes: 3
Reputation: 1817
Call m_vbuffer->reserve(SIZE_OF_BUFFER) in the constructor of SocketClient.
Upvotes: 1
Reputation: 16761
In addition to other answers consider using some circular buffer instead of vector. There is one in boost.
Upvotes: 0