CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2012
    Posts
    58

    static const class instances

    hi,

    say i have this rather large class, which (in a way) somehow resembles a custom dialog control). This control is supposed to display data, which it does just fine. To do so, it maintains a

    byte settings[10];

    array, which holds information on how to display the data.

    There are multiple ways to represent this custom set of data.
    In order to remain flexible in representing it, i thought of implementing some sort of DisplayProvider, which can be registered to the base class and provides that settings byte array.

    Preferably, i would now have a set of static const instances of this provider.
    Using a struct would work nicely here:

    PHP Code:
    struct DisplayProvider
    {
    int settings[10];
    }

    static const 
    DisplayProvider prov1 = {1,2,3,4,5,6,7,8,9,0}; 

    The problem: The DisplayProvider would have to do some pre-processing, before handing over control to the base class, which then does the main work.
    I would end up with something like this:


    PHP Code:
    class DispalyProvider
    {
    baseclassowner;
     
    int settings[10];
    void PreProcessing(...);//ends up calling the owner.Processing(...) function
    }; 

    I am not the greatest fan of having that owner pointer, but there` s probably no way around that.
    The main thing here is, that i dont really see a way to create a stock of default "static const DisplayProvder = {...}"s, as i could when using a struct.

    Note that this a simplified example, i hope it shows the problem well enough...because i dont really see any good solution to this. :S

  2. #2
    Join Date
    Jun 2012
    Posts
    58

    Re: static const class instances

    Ok, so the previous post boils down to

    Is it possible to have class instance with different methods, and that`s ofcourse nonsense.
    OK. I`ll keep thinking about this, see you tomorrow...

  3. #3
    Join Date
    Jun 2012
    Posts
    58

    Re: static const class instances

    edit: nope
    Last edited by tuli; June 18th, 2012 at 01:50 AM.

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: static const class instances

    Didn't follow you 100%, but that sounds like a job for a singleton, no?
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: static const class instances

    Quote Originally Posted by tuli View Post
    The problem: The DisplayProvider would have to do some pre-processing, before handing over control to the base class, which then does the main work.
    What you are saying here doesn't make much sense (to me). A struct or class defines a type. A type cannot actually do something, only functions can do something.
    I think you need to explain better at which point in your program you need to do preprocessing, what needs to be preprocessed, and where the preprocessed data then needs to be used.
    Quote Originally Posted by tuli View Post
    I would end up with something like this:
    PHP Code:
    class DispalyProvider
    {
    baseclassowner;
     
    int settings[10];
    void PreProcessing(...);//ends up calling the owner.Processing(...) function
    }; 
    This doesn't make much sense either. If you have const instances of DisplayProvider then you cannot call the member function on them, since it is not a const member function. If you would make the member function const, it would seem to be a no-op, since it cannot modify the member variable.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    Join Date
    Jun 2012
    Posts
    58

    Re: static const class instances

    you are, of course, right. It doesnt make much sense.

    I used to have something like


    PHP Code:
    class x
    {
    ...
    m_state;
    ...
    void processing(...)
    {
    switch(
    state)
    {
    case 
    1:
    something
    case 2:
    someting else
    ...
    }

    //something generic here...


    As you can see, first something state-specific is done in processing(), then something generic.

    In order to be able to easily extend the avalable states, i would have liked to define a "plug-in class", a "state-provider".
    I`ve come to realise a struct is not enough to do this in this case, so here is what i do now:

    PHP Code:
    class statebase
    {
    xowner;
    virtual void processing(...);
    ...
    }

    class 
    state1view : public statebase
    {
    void processing(...)
    {
    //state specific
    x->processing(...);//generic stuff
    }
    ...


    which works just fine.
    No more static const struct, but that just wasnt enough here, i think.

    thanks for all your help!

  7. #7
    Join Date
    Mar 2001
    Posts
    2,529

    Re: static const class instances

    Here is a simple example of the Singleton:

    http://forums.codeguru.com/showthread.php?t=344782

    The Singleton is a design pattern that models the behavior and anatomy of the Singleton.
    Last edited by ahoodin; June 18th, 2012 at 10:01 AM.
    ahoodin
    To keep the plot moving, that's why.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: static const class instances

    Quote Originally Posted by tuli View Post
    you are, of course, right. It doesnt make much sense.

    I used to have something like
    One word of advice when posting code:

    1) Don't use PHP tags. There is no need to post code that lights up like a Christmas tree.

    2) Indent code properly.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured