Static vars in C++

2

Comments

  • : : [b]Hi IDK[/b],
    : :
    : : I think if you learn more about Object Oriented [b][italic]coding[/b][/italic] in C++, and use a good compiler, many of your problems will be solved, without any Q/A!
    : :
    :
    : That's what I'm trying to do now. Asking Qs are one way to learn.
    : If you know any good reference to this I will gladly use it, but I cannot find any. All my books have nothing on this, and the net isn't much help either.
    :
    : And about the compiler, have you ever heard of gcc?
    : gcc is THE compiler.
    :


    Yep, gcc is conforming to the standards nicely. And while you wait for the compiler to finish chewing at your code, you can always download and install Borland ;-)


  • :
    : Yep, gcc is conforming to the standards nicely. And while you wait for the compiler to finish chewing at your code, you can always download and install Borland ;-)
    :
    :

    It's not too bad with make.

    Also there's some optimised versions, which I haven't tried, but they should be a lot faster.

    Does Borland have a compiler for x86 linux?
  • : : The problem is now that I cannot access blocksize within the ramblock class.
    : :
    : : Here's the code:
    : :
    : : Ramblock::Ramblock(){
    : : if((data = new char[blocksize]) == NULL) per("(m)allocation fault");
    : : }
    : :

    --------------------------------------------------------------------------------


    : :
    :
    :
    : so, just change it to protected and you won't have any problems.
    :

    OK, so I changed it to:

    class Ramblock : protected DataBlock{


    --------------------------------------------------------------------------------



    But now I can't access it outside of the class.

    block.h:10: error: 'static void DataBlock::printSize()' is inaccessible
    test.cpp:6: error: within this context


    --------------------------------------------------------------------------------



    :Shouldn't public make it public?
    :And protected should make it protected, ie, not accessible at all.
    :Now public makes it public but protected in the class, and protected :makes it private.
    :What does private do?
    :This is wierd.

    :EDIT: private does the same as protected.

    :Do anyone know a good reference to this?
    :None of my books has it in them and I can't find anything on the net.


    No that is not what I meant.
    [code]
    class DataBlock{
    [red]protected:[/red]
    static int blocksize;
    public:
    static void setBlocksize(int i){ blocksize = i; }
    static void printSize(){ printf("%d
    ",blocksize); }
    virtual char operator [](int) = 0;
    };

    [/code]




  • [b]Hi IDK[/b],
    I have the 'C++ Primer 3rd'(2.37 MB) E-Book, if you want that, email me and I will send it to your email.
  • : : make the base class in each derived class public. That fixed the problems with my compiler.
    : : [code]
    : : class Ramblock : public DataBlock{
    : :
    : :
    : : class Fileblock : public DataBlock{
    : :
    : : [/code]
    : :
    :
    : Check my edit I did before...
    :
    : The problem is now that I cannot access blocksize within the ramblock class.
    :
    : Here's the code:
    : [code]
    : Ramblock::Ramblock(){
    : if((data = new char[blocksize]) == NULL) per("(m)allocation fault");
    : }
    : [/code]
    :

    Hi,

    It seems like you did everything but didn't understand what you were doing at all. What you were doing was private inheritance which will cause problems if you really want public inheritabce.

    Secondly private data is private - meaning even a derived class can't access it, however protected means that derived class can access it and modify it - in effect you derived class must not be getting the data from it - instead you should have a function which gets the static data - like

    [code]
    class Super {
    public :
    static int setsize(int i);
    int GetSize() const { return blocksize; }
    ...

    private :
    static int size;
    };

    int Super::size = 0;

    [/code]

    Lastly why do you need the last line ? This is to resolve the class the variable belongs to because in essence it is in the global area of the executeable. Hence the linker errors if you don't do it.
  • :
    : :
    : : Yep, gcc is conforming to the standards nicely. And while you wait for the compiler to finish chewing at your code, you can always download and install Borland ;-)
    : :
    : :
    :
    : It's not too bad with make.
    :
    : Also there's some optimised versions, which I haven't tried, but they should be a lot faster.
    :
    : Does Borland have a compiler for x86 linux?
    :


    I think they have something called Kylix which is supposed to work with both Linux and Windows, if their advertising is true.
  • : : Does Borland have a compiler for x86 linux?
    : :
    :
    :
    : I think they have something called Kylix which is supposed to work with both Linux and Windows, if their advertising is true.
    :

    No, I think Kylix is just another name for Delphi on Linux...
    But I haven't looked it up...
  • [b][red]This message was edited by Gregry2 at 2006-9-26 8:44:52[/red][/b][hr]
    : : : Does Borland have a compiler for x86 linux?
    : : :
    : :
    : :
    : : I think they have something called Kylix which is supposed to work with both Linux and Windows, if their advertising is true.
    : :
    :
    : No, I think Kylix is just another name for Delphi on Linux...
    : But I haven't looked it up...
    :

    It a Linux port of both Delphi and C++ Builder...

    theres even a wikipedia on it

    http://en.wikipedia.org/wiki/Kylix_programming_tool

    {2}rIng

    EDIT: Wow, it sounds real horrid...

  • : Hi,
    :
    : It seems like you did everything but didn't understand what you were doing at all. What you were doing was private inheritance which will cause problems if you really want public inheritabce.
    :

    I want private inherence of the variable, and public of the funcs.

    : Secondly private data is private - meaning even a derived class can't access it, however protected means that derived class can access it and modify it - in effect you derived class must not be getting the data from it - instead you should have a function which gets the static data - like

    I thought it was the other way around. Thanks!

    :
    : [code]
    : class Super {
    : public :
    : static int setsize(int i);
    : int GetSize() const { return blocksize; }
    : ...
    :
    : private :
    : static int size;
    : };
    :
    : int Super::size = 0;
    :
    : [/code]
    :
    : Lastly why do you need the last line ? This is to resolve the class the variable belongs to because in essence it is in the global area of the executeable. Hence the linker errors if you don't do it.
    :

    I don't know, it was Mohammad Rast that suggested it.

    I don't even know what it is and didn't understand your explanation, could you refrase it or something.
  • : No that is not what I meant.
    : [code]
    : class DataBlock{
    : [red]protected:[/red]
    : static int blocksize;
    : public:
    : static void setBlocksize(int i){ blocksize = i; }
    : static void printSize(){ printf("%d
    ",blocksize); }
    : virtual char operator [](int) = 0;
    : };
    :
    : [/code]
    :
    :

    Here's my current code:

    [code]
    class DataBlock{
    protected:
    static int blocksize;
    public:
    static void setBlocksize(int i){ blocksize = i; }
    static void printSize(){ printf("%d
    ",blocksize); }
    virtual char operator [](int) = 0;
    };

    //int DataBlock::blocksize;

    class Ramblock : public DataBlock{
    char *data;

    public:
    Ramblock();
    ~Ramblock();

    char operator[](int);
    // void Readblock(FILE*,int);
    };

    class Fileblock : public DataBlock{
    FILE* file;
    public:
    Fileblock(char*);
    ~Fileblock();

    char operator[](int);
    void Readblock(FILE*,int);
    };
    [/code]

    Now compiling works!!!

    The linking is another thing, nothing works.

    [code]
    test.o: In function `DataBlock::setBlocksize(int)':
    test.cpp:(.text._ZN9DataBlock12setBlocksizeEi[DataBlock::setBlocksize(int)]+0x7): undefined reference to `DataBlock::blocksize'
    test.o: In function `DataBlock::printSize()':
    test.cpp:(.text._ZN9DataBlock9printSizeEv[DataBlock::printSize()]+0x7): undefined reference to `DataBlock::blocksize'
    ramblock.o: In function `Ramblock::Ramblock()':
    ramblock.cpp:(.text+0xe6): undefined reference to `DataBlock::blocksize'
    ramblock.o: In function `Ramblock::Ramblock()':
    ramblock.cpp:(.text+0x134): undefined reference to `DataBlock::blocksize'
    [/code]
  • : :
    : : void main() // or main()
    : : {
    :
    :
    : main ALWAYS returns an int and should be declared that way
    : [code]
    : [red]int main() [/red]
    : [/code]
    :
    : C might allow void main() but c++ will REQUIRE int main().
    :

    People should always declare main() as returning int even in C. Some compilers might tolerate the absense of int in the declaration in C.

    ie.
    main()
    {
    }

    but most will not tolerate people putting stuff other than an integer as the return type for main
  • : [b][red]This message was edited by Gregry2 at 2006-9-26 8:44:52[/red][/b][hr]
    : : : : Does Borland have a compiler for x86 linux?
    : : : :
    : : :
    : : :
    : : : I think they have something called Kylix which is supposed to work with both Linux and Windows, if their advertising is true.
    : : :
    : :
    : : No, I think Kylix is just another name for Delphi on Linux...
    : : But I haven't looked it up...
    : :
    :
    : It a Linux port of both Delphi and C++ Builder...
    :
    : theres even a wikipedia on it
    :
    : http://en.wikipedia.org/wiki/Kylix_programming_tool
    :
    : {2}rIng
    :
    : EDIT: Wow, it sounds real horrid...
    :


    When using Borland, Delphi == C++ Builder :-)
    Builder is actually running Object Pascal code.

  • : [code]
    : class Super {
    : public :
    : static int setsize(int i);
    : int GetSize() const { return blocksize; }
    : ...
    :
    : private :
    : static int size;
    : };
    :
    : int Super::size = 0;
    :
    : [/code]
    :
    : Lastly why do you need the last line ? This is to resolve the class the variable belongs to because in essence it is in the global area of the executeable. Hence the linker errors if you don't do it.
    :


    The last line is how you initialize static members in C++.
    There is no other way to do so. You may get linker errors if you [b]don't[/b] do it:

    http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.11

  • Thanks, that solved the problem, but created another:

    I'll let gcc speak for it self:
    [code]
    fileblock.o:(.data+0x0): multiple definition of `DataBlock::blocksize'
    test.o:(.data+0x0): first defined here
    ramblock.o:(.data+0x0): multiple definition of `DataBlock::blocksize'
    test.o:(.data+0x0): first defined here
    [/code]

    I've included block.h, where I have the class definitions, in fileblock.cpp, ramblock.cpp and test.cpp.


    And another thing:
    Quote from the link you gave me:[code]
    The usual place to define static data members of class Fred is file Fred.cpp (or Fred.C or whatever source file extension you use).
    [/code]

    Why should they be placed in the cpp, woulnd't it be better to have them in the header, so one wont forget about declaring it?
  • : Thanks, that solved the problem, but created another:
    :
    : I'll let gcc speak for it self:
    : [code]
    : fileblock.o:(.data+0x0): multiple definition of `DataBlock::blocksize'
    : test.o:(.data+0x0): first defined here
    : ramblock.o:(.data+0x0): multiple definition of `DataBlock::blocksize'
    : test.o:(.data+0x0): first defined here
    : [/code]
    :
    : I've included block.h, where I have the class definitions, in fileblock.cpp, ramblock.cpp and test.cpp.


    [blue]Would it be possible to post the whole modified code again?[/blue]


    : And another thing:
    : Quote from the link you gave me:[code]
    : The usual place to define static data members of class Fred is file Fred.cpp (or Fred.C or whatever source file extension you use).
    : [/code]
    :
    : Why should they be placed in the cpp, woulnd't it be better to have them in the header, so one wont forget about declaring it?

    [blue]I like to think of the h-file as a document provided to an other programmer to show him what to expect from your class. Since private statics are of no interest outside of the class, there is no reason to show them in the h-file.

    In C it is even more obvious, you -can't- place variable declarations/initializations in the h-file or you'll be in trouble when more than 1 .c file includes that h-file.

    [/blue]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories