-5

Why is this syntax not correct?

 char* p = new char[dg.sizeof(Payload())];

I want to make a new variable that is a string the size of the Payload portion of dg... I don't understand how to do this? Thank you

 error: expected unqualified-id before 'sizeof'
 error: expected `]' before 'sizeof'
 error: expected ',' or ';' before 'sizeof'
Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
user2616248
  • 41
  • 1
  • 4

3 Answers3

4

sizeof is not a function, it's an operator like + or *, but it operates at compile time. It takes one operand, either a typename in parens, or an expression (which need not be in parens). So if Payload is a member of structure dg, sizeof dg.Payload is the size of the function pointer member, and sizeof dg.Payload() is the size of Payload()'s return type.

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
2

sizeof is a c/c++ operator. It cannot be used as a class method. http://en.cppreference.com/w/cpp/language/sizeof

Robert Jacobs
  • 3,266
  • 1
  • 20
  • 30
1

Change

char* p = new char[dg.sizeof(Payload())];

to

char* p = new char[sizeof(dg.Payload())];
huysentruitw
  • 27,376
  • 9
  • 90
  • 133
JHobern
  • 866
  • 1
  • 13
  • 20
  • 1
    I doubt `sizeof` is the right tool at all though. Looks like a function trying to return an array. – Quentin Aug 27 '15 at 17:24
  • 2
    Note that this will return the sizeof() the return type of Payload, which may or may not be what the OP wants. OP seems to have a fundamental misunderstanding of what sizeof is for. – JPhi1618 Aug 27 '15 at 17:24
  • 2
    We don't have enough context yet, but I'm already sure this answer is wrong ;) – huysentruitw Aug 27 '15 at 17:25
  • @WouterHuysentruit: Following up the "missing context" you mention the answer cannot be wrong, nor right ... ;-) - I however agree on that it might not solve the OP's issue ... – alk Aug 27 '15 at 17:44