Results 1 to 12 of 12

Thread: new

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default new

    hi I need something about this (inside a class). Is there a way??
    Qt Code:
    1. class prove {
    2. private:
    3. int size;
    4. char* save[size];
    5. }
    To copy to clipboard, switch view to plain text mode 
    thanks
    Regards

  2. #2
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: new

    What you want is a dynamic array. But both C++ and Qt have various data-structures you can use instead. std::vector, QVector, QList...
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: new

    hi, I donn't want a dynamic array. I'm looking for a way to put "save" as member of a class to permit to all members to use that space of memory....Are you understand? I don't like declare it as global (anyway: at begin of program I don't know how size is large....
    Regards

  4. #4
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: new

    Then you still want a dynamic array. Look it up.

    I'm guessing you want all instances of the class to be able to use the memory. Make it static.

    static char** save;
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  5. #5
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: new

    Do you need something like this...
    Qt Code:
    1. class A
    2. {
    3. public:
    4. enum {size = 100};
    5. A(){}
    6. private:
    7. char* save[size];
    8. };
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: new

    I don't think so, because:

    Quote Originally Posted by mickey View Post
    at begin of program I don't know how size is large
    By the way, if you want a constant, why not just say: const int size = 100;
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  7. #7
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: new

    Quote Originally Posted by mickey View Post
    hi, I donn't want a dynamic array. I'm looking for a way to put "save" as member of a class to permit to all members to use that space of memory....Are you understand? I don't like declare it as global (anyway: at begin of program I don't know how size is large....
    Why not use string in that case, why you dont want a builtin type (like string, vectore, list ). IS there any particular reason????

    Rather than using a char*, it is always better to you std::string.

  8. #8
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: new

    But a char** is not a string, and that's what he says he wants here. I guess he just wants to allocate a certain amount of memory. I still say: dynamic array.

    However, maybe mickey should explain more clearly what it is he wants here.

    (By the way, pet peeve of mine: don't use more than one question mark. It serves no purpose.)
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  9. #9
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: new

    mickey-->

    Are u looking something like this
    Qt Code:
    1. char *pChar;
    2. string sStr;
    3. cout<<"Please Enter The String ";
    4. cin>>sStr; //string input
    5. pChar = new char[sStr.length() + 1];
    6. strcpy(pChar, sStr.c_str());
    7. delete []pChar;
    To copy to clipboard, switch view to plain text mode 

    I'm not yet clear, actually what is that you are looking for. The above example only uses char*. Please explain what is that you want....

  10. #10
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: new

    Probably you want something like this
    Qt Code:
    1. class A
    2. {
    3. public:
    4. A() { save = 0; }
    5. void allocate()
    6. {
    7. //assuming size is known by now
    8. save = new char*[size]; // allocate pointers
    9. for(int i=0; i < size; i++)
    10. save[i] = new char[whatEverSizeYouNeed];
    11. }
    12. private:
    13. int size;
    14. char **save;
    15. };
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  11. #11
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: new

    I did a mistake in the first post. I wanted something like this below:
    Qt Code:
    1. class A
    2. {
    3. public:
    4. int size;
    5. A(){}
    6. private:
    7. char* save;
    8. };
    9. A::A (int sz) {
    10. size = sz;
    11. save = new char [size]
    12. }
    To copy to clipboard, switch view to plain text mode 
    Regards

  12. #12
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: new

    So what's the problem? Looks like it should work. If your constructor has a parameter, at least. You have one in your implementation, but not in your class definition.

    Also, why is A::size public? Do you ever want to change the size of the string after the object has been created?

    If so, a public variable is not enough (and a public variable is always bad style). You need to create a resize(uint size) function. This function should create a new dynamic array with the new size, copy all old values into it, delete the old array and let A::save point to the new array. There is no other way.

    Alternatively, if you just want a string with a dynamic size, there are loads of classes that already do this for you. std::string and QString, for example.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.