"invalid in-class initialization" Why can't I do this?

This is a discussion on "invalid in-class initialization" Why can't I do this? within the C++ Programming forums, part of the General Programming Boards category; Why is this invalid? Code: class T { jclass myClass = NULL; };...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    "invalid in-class initialization" Why can't I do this?

    Why is this invalid?

    Code:
    class T
    {
        jclass myClass = NULL;
    };

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,810
    Because assignment would make 'myClass' static. (And a non-const static member is only a declaration--meaning it must be declared and assigned outside of the class.)

    Soma

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you want it to be a regular member, you would do this in all of your constructors:
    Code:
    class T
    {
        T() : myClass(NULL) { }
    };
    Although it really depends on jclass and how it can be constructed. I have a feeling you can't just assign or initialize it with NULL unless it was a pointer.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Huh? Marking myClass static would make it static. Assignment has nothing to do with it.

    It's invalid because the designers of C++ didn't include it in the C++ standard. The reasons are probably lost in time. Perhaps nobody thought of suggesting it. Perhaps it was deemed redundant, and the complexity of defining the semantics in relation to initializer lists not worth it. Perhaps people thought that not having all initialization in the same place (the constructor initializer list) would make the code harder to read.

    Java's designers added direct initializers, but subtle aspects guiding such a decision are very different in Java than in C++.

    I'm quite sure there was a proposal to add Java-style direct initializers to C++. I wonder what happened to it ...
    Ah, found it.
    Seems like it will be in the next standard.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,810
    Marking myClass static would make it static. Assignment has nothing to do with it.
    Has everything to do with it, though I may have said it in a very strange way.

    Assignment of a structure members inline, in the tradition of C syntax, actually requires 'static'; everything else you see is actually an extension--that virtually every compiler allows.

    Edit: Ah, I just realized that this is the same person... so this isn't a class but a pointer to a class.

    Soma
    Last edited by phantomotap; 04-17-2008 at 05:06 PM.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Is jclass a pointer?

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,810
    Is jclass a pointer?
    Apparently:

    How would I achieve this dynamic_cast?

    Soma

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Assignment of a structure members inline, in the tradition of C syntax, actually requires 'static';
    So you mean, if the OP has seen inline assignment, it was for statics?

    OK, that makes a lot more sense. But might as well be complete: inline assignment works for static integral constants, nothing else.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by cpjust View Post
    Is jclass a pointer?
    It is. They hide it by using typedef but it's a pointer.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,172
    Even one more reason typedefs are so incredibly evil. There was no hint that it was a pointer here. Misleading typedef name, as well.
    Don't do it! In this case, it's far better to not use a typedef, even if it means not being consistent.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21