inhibit use of class/struct as auto variabel

Discussion in 'C++' started by 42Bastian Schick, Nov 29, 2010.

  1. Hi,

    is there a way to inhibt the use of a struct/class as auto or static
    variable other than code-review. Means enforce usage of new() to
    allocate an object ?

    If this is common knowledge, please excuse, but I come from assembler
    :)

    TIA
    --
    42Bastian
    Do not email to , it's a spam-only account :)
    Use <same-name>@monlynx.de instead !
     
    42Bastian Schick, Nov 29, 2010
    #1
    1. Advertising

  2. On 11/29/2010 9:07 AM, 42Bastian Schick wrote:
    > is there a way to inhibt the use of a struct/class as auto or static
    > variable other than code-review. Means enforce usage of new() to
    > allocate an object ?
    >
    > If this is common knowledge, please excuse, but I come from assembler
    > :)


    It is common knowledge. Read up on Factory Method and private
    constructors. And next time do specify whether the question you're
    asking is for a job interview or a homework. Makes it easier for us to
    give you the right answer.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 29, 2010
    #2
    1. Advertising

  3. * 42Bastian Schick, on 29.11.2010 15:07:
    > Hi,
    >
    > is there a way to inhibt the use of a struct/class as auto or static
    > variable other than code-review. Means enforce usage of new() to
    > allocate an object ?
    >
    > If this is common knowledge, please excuse, but I come from assembler
    > :)


    The easiest and most practical way is to make the destructor 'protected' and
    define a single for-all-types 'destroy' template function, unless you're
    targeting C++0x only in which case better standardize on the one for unique_ptr.

    The most well known way is, however, to make all constructors 'protected' (or
    'private') and define corresponding factory functions.

    And apparently paradoxically, most programmers prefer the factories way. It
    doesn't make sense in economic terms (more work for same effect, more code to
    maintain). But I think it has to do with their feeling of accomplishment, simply
    that the protected destructor way is so easy that they feel they haven't really
    done anything. Same reason as people use raw arrays instead of std::vector &
    friends, and so on. Most programmers prefer The Hard Way...


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Nov 29, 2010
    #3
  4. On Mon, 29 Nov 2010 09:15:11 -0500, Victor Bazarov
    <> wrote:

    >On 11/29/2010 9:07 AM, 42Bastian Schick wrote:
    >> is there a way to inhibt the use of a struct/class as auto or static
    >> variable other than code-review. Means enforce usage of new() to
    >> allocate an object ?
    >>
    >> If this is common knowledge, please excuse, but I come from assembler
    >> :)

    >
    >It is common knowledge. Read up on Factory Method and private
    >constructors.


    Ok. I'll check this out.

    > And next time do specify whether the question you're
    >asking is for a job interview or a homework.


    Neither. But 99% of my time I program in pure assembly and since
    "factory" is nothing I bring into relation with C++ it is hard to
    google for it.

    > Makes it easier for us to
    >give you the right answer.




    --
    42Bastian
    Do not email to , it's a spam-only account :)
    Use <same-name>@monlynx.de instead !
     
    42Bastian Schick, Nov 29, 2010
    #4
  5. On Mon, 29 Nov 2010 15:59:47 +0100, "Alf P. Steinbach /Usenet"
    <> wrote:

    >* 42Bastian Schick, on 29.11.2010 15:07:
    >> Hi,
    >>
    >> is there a way to inhibt the use of a struct/class as auto or static
    >> variable other than code-review. Means enforce usage of new() to
    >> allocate an object ?
    >>
    >> If this is common knowledge, please excuse, but I come from assembler
    >> :)

    >
    >The easiest and most practical way is to make the destructor 'protected' and
    >define a single for-all-types 'destroy' template function, unless you're
    >targeting C++0x only in which case better standardize on the one for unique_ptr.


    Ok, I tried it, and to be honest, I do not understand, why if I make
    the destructor privat, I ...

    Oh, clear, now I got it. The compiler wants to call the destructor at
    the end of the function but can't !

    Cool.

    --
    42Bastian
    Do not email to , it's a spam-only account :)
    Use <same-name>@monlynx.de instead !
     
    42Bastian Schick, Nov 29, 2010
    #5
  6. On Mon, 29 Nov 2010 09:15:11 -0500, Victor Bazarov
    <> wrote:

    >On 11/29/2010 9:07 AM, 42Bastian Schick wrote:
    >> is there a way to inhibt the use of a struct/class as auto or static
    >> variable other than code-review. Means enforce usage of new() to
    >> allocate an object ?
    >>
    >> If this is common knowledge, please excuse, but I come from assembler
    >> :)

    >
    >It is common knowledge. Read up on Factory Method and private
    >constructors.


    Meanwhile tried the factory thing and it works.

    Now I have to check which method (factory or Alf's) does suit my needs
    better.

    Thanks for you time.

    --
    42Bastian
    Do not email to , it's a spam-only account :)
    Use <same-name>@monlynx.de instead !
     
    42Bastian Schick, Nov 29, 2010
    #6
  7. 42Bastian Schick

    James Kanze Guest

    On Nov 29, 2:59 pm, "Alf P. Steinbach /Usenet" <alf.p.steinbach
    > wrote:
    > * 42Bastian Schick, on 29.11.2010 15:07:


    > > is there a way to inhibt the use of a struct/class as auto or static
    > > variable other than code-review. Means enforce usage of new() to
    > > allocate an object ?


    > > If this is common knowledge, please excuse, but I come from assembler
    > > :)


    > The easiest and most practical way is to make the destructor
    > 'protected' and define a single for-all-types 'destroy'
    > template function,


    Can you expound on that? I don't see how making the destructor
    protected will make it accessible to a single for-all-types
    destroy function.

    Generally, if the class is designed to be a base, I'd make the
    destructor protected; otherwise, I'd make it private. But more
    generally... any given class has specific semantics. And users
    use the class in accordance with its semantics. If the
    semantics require it to be dynamically allocated, I've yet to
    encounter programmer who would attempt to allocate it on the
    stack. It's one of those errors that are so rare, they aren't
    worth bothering with.

    --
    James Kanze
     
    James Kanze, Nov 29, 2010
    #7
  8. On Mon, 29 Nov 2010 10:47:24 -0800 (PST), James Kanze
    <> wrote:

    >On Nov 29, 2:59 pm, "Alf P. Steinbach /Usenet" <alf.p.steinbach
    >> wrote:
    >> * 42Bastian Schick, on 29.11.2010 15:07:

    >
    >> > is there a way to inhibt the use of a struct/class as auto or static
    >> > variable other than code-review. Means enforce usage of new() to
    >> > allocate an object ?

    >
    >> > If this is common knowledge, please excuse, but I come from assembler
    >> > :)

    >
    >> The easiest and most practical way is to make the destructor
    >> 'protected' and define a single for-all-types 'destroy'
    >> template function,

    >
    >Can you expound on that? I don't see how making the destructor
    >protected will make it accessible to a single for-all-types
    >destroy function.
    >
    > ... If the
    >semantics require it to be dynamically allocated, I've yet to
    >encounter programmer who would attempt to allocate it on the
    >stack. It's one of those errors that are so rare, they aren't
    >worth bothering with.


    The real-world problem is a message passing RTOS, where messages a
    piece of memory overlaid with structs. Though one can tell the
    programmer not to "allocate" such message on a stack, a enforcement by
    the compiler is much better then doing a code-review.



    --
    42Bastian
    Do not email to , it's a spam-only account :)
    Use <same-name>@monlynx.de instead !
     
    42Bastian Schick, Nov 29, 2010
    #8
  9. * James Kanze, on 29.11.2010 19:47:
    > On Nov 29, 2:59 pm, "Alf P. Steinbach /Usenet"<alf.p.steinbach
    > > wrote:
    >> * 42Bastian Schick, on 29.11.2010 15:07:

    >
    >>> is there a way to inhibt the use of a struct/class as auto or static
    >>> variable other than code-review. Means enforce usage of new() to
    >>> allocate an object ?

    >
    >>> If this is common knowledge, please excuse, but I come from assembler
    >>> :)

    >
    >> The easiest and most practical way is to make the destructor
    >> 'protected' and define a single for-all-types 'destroy'
    >> template function,

    >
    > Can you expound on that? I don't see how making the destructor
    > protected will make it accessible to a single for-all-types
    > destroy function.


    C++ has a wonderful solution for that, James. It's called "friend". I suggest
    reading about C++ "friend" in your textbook.

    Instead of directly using "friend" in every such class, you can use another
    wonderful language feature called "inheritance". This one's really cool! You can
    "inherit" in all kinds of functionality, including destroy functionality.

    I think you'll find that your textbook also covers the concept of "inheritance".

    :)


    > Generally, if the class is designed to be a base, I'd make the
    > destructor protected; otherwise, I'd make it private. But more
    > generally... any given class has specific semantics. And users
    > use the class in accordance with its semantics. If the
    > semantics require it to be dynamically allocated, I've yet to
    > encounter programmer who would attempt to allocate it on the
    > stack. It's one of those errors that are so rare, they aren't
    > worth bothering with.


    If you're learning C++ coming from Python, say, then you're in for a little
    shock! The philosophy in languages like Python is to provide other programmers
    with as much access to things as possible, so that they can do whatever they
    want. This was and is also the philosophy of the language called "C". But it
    turned out that for larger programs it's Really Helpful to have the compiler
    catch errors, instead of having to try to reproduce errors at run time. And in
    order to enable the compiler to catch errors, C++ provides all kinds of means
    for *restricting* what the programmer can do (that's the "++" in "C++"), telling
    the compiler that if the programmer does that, or that, hey, diagnose that as an
    error, please. It's a completely different philosophy.

    It might seem strange at first, but consider the Unix idea of files as simply
    streams of bytes. This *restriction* on what a file can be, enabled a lot of
    useful functionality where different programs could be fitted together; they
    could more easily work with each others' data.

    So, somewhat paradoxically, good restrictions help both to catch errors
    automatically, and help to enable functionality. Bad restrictions are, however,
    bad -- some intelligence and thoughtfulness is required in order to choose the
    good restrictions and don't choose the bad ones. C++ lets you enforce good
    restrictions, but, thereby, it also lets you enforce bad ones: take care!


    Cheers & hth.,

    - Alf


    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Nov 30, 2010
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert

    How do I inhibit warning 528?

    Robert, Jan 23, 2004, in forum: C Programming
    Replies:
    15
    Views:
    1,004
    Mark McIntyre
    Jan 29, 2004
  2. linkswanted
    Replies:
    1
    Views:
    970
  3. hon123456
    Replies:
    3
    Views:
    411
    hon123456
    Dec 18, 2009
  4. Ronj

    Inhibit Datasource fill on pageLoad

    Ronj, Aug 7, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    158
  5. Matt Victorio
    Replies:
    2
    Views:
    139
    Brian Candler
    Feb 24, 2011
Loading...

Share This Page