Messy but C++, a lot of pointers.

This is a discussion on Messy but C++, a lot of pointers. within the C++ Programming forums, part of the General Programming Boards category; Given these pseudo C++ class definitions: Code: class Request public: char* name; ArrayOf_Property* properties; class ArrayOf_Property public: Property** ptr; int ...

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    Messy but C++, a lot of pointers.

    Given these pseudo C++ class definitions:
    Code:
    class Request
    public:
        char* name;
        ArrayOf_Property* properties;
       
    class ArrayOf_Property
    public:
        Property** ptr;
        int size;
       
    Property
    public:
        char *key;
        char *value;
    is the following correct initialization? (do not worry delete, i will do that later):
    Code:
    Request* req = new Request;
    req->properties = new ArrayOf_Property;
    req->name = new char[128]
    int size = 2;
    Property** items = new Property*[size];
    for (int i=0; i<size; i++) {
        items[i] = new Porperty;
    }
    
    items[0]->key = new char[128];
    items[0]->value = new char[128];
    
    items[1]->key = new char[128];
    items[1]->value = new char[128];
    
    req->properties->ptr = items;
    req->properties->size = size;

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is one possible interpretation, yes. But certainly not the only "valid" option.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Thank you Mats.
    Can you tell me other ways? Are there better, simpler, more elegant ways?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not sure, I'm just trying to say that the definition of data-types in themselves does not declare how they should be used. So if you give us a bunch of data types, and then some code that supposedly set those up, then all we can say is whether what you are doing is legal with respects to the data types. I suspect this has to do with a previous thread on properties and gSoap(?) that you posted earlier. My point being that just because you are posting code that COULD work, doesn't mean that it WILL work in conjunction with other code that interprets the values you have put into the data structures.

    And just as one example of how you COULD make it different - that's by no means saying it's more or less correct than what you posted - just different:
    Code:
    Property items[2] = { {"Key1", "Value1"}, {"Key2", "Value2"}};
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Thank you Mats, for quick reply, when it was a little urgent for me also.

    Yeah, I understand now.
    Ok. As my initialization is correct. I will use them in my request. You are right this is gSOAP related. They changed their WSDL file. So I have new format for same request. I just wanted to make sure, that it is properly initialized, before messing with the server.
    Last edited by manav; 06-02-2008 at 06:46 AM.

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Instead of char* use Strings, instead of array** use vector<type*> etc.

    C++ gives you the tools, use them.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,180
    Manav knows does, but the crapware he is forced to use generates this crap-code, so he has to live with it.
    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.
    よく聞くがいい!私は天才だからね! ^_^

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    Manav, can you post a small example WSDL I can play with? To the best of my knowledge, you should be able to use strings and vectors in gSOAP.

  9. #9
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Riches, thank you for your help.
    I ran the wsdl2h file with -s flag to create non STL code. Actually, when I did not use the flag it created all std::string* in the output. Which was incompatible with other previous requests that I used to make.

    I can give you that part of the WSDL file where the above definitions occur:
    [Original file is too big, plus I do not know, if, even for my own help, I should give the commercially used file on public forums]
    Code:
    <complexType name="UpdateUserPropertiesRequest">
        <complexContent>
         <extension base="tns1:SimpleRequest">
          <sequence>
           <element name="collanosName" nillable="true" type="xsd:string"/>
           <element name="properties" nillable="true" type="impl:ArrayOf_tns1_Property"/>
          </sequence>
         </extension>
        </complexContent>
       </complexType>
    Code:
    <complexType name="ArrayOf_tns1_Property">
        <complexContent>
         <restriction base="soapenc:Array">
          <attribute ref="soapenc:arrayType" wsdl:arrayType="tns1:Property[]"/>
         </restriction>
        </complexContent>
       </complexType>
    Code:
    <complexType name="Property">
        <sequence>
         <element name="key" nillable="true" type="xsd:string"/>
         <element name="value" nillable="true" type="xsd:string"/>
        </sequence>
       </complexType>

  10. #10
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    I think I will need the definition of tns1:SimpleRequest, as well as the message and operation definitions for the UpdateUserProperties function that I assume used this UpdateUserPropertiesRequest type, to compile.

    So if I understand correctly, you do not want to use the std::string type, but do want vectors?

    EDIT: Actually, the namespace definitions would be helpful as well.
    Last edited by rags_to_riches; 06-03-2008 at 06:09 AM. Reason: Addition

  11. #11
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Riches,
    I have no preference regarding std::string* / char* or std::vector, the problem was all the previously coded requests would use, char*, but I was getting std::string*, so, I had to use -s flag for wsdl2h command. As you know, what matters is the end product, the XML which should be made according to WSDL specifications. I would change previous code as well, but the thing is, that, std::string* is still messy, so char* would not hurt either. And it was backwards compatible (so to speak), with older code.

    Thanks for all your help Riches.
    I think only full WSDL would be better for you. But as I am unable to give that, and partial definitions are more work for you. So I do not wish to bother you more.

    Actually, the above request is working fine now. It is producing the XML in exact format as required on server side.

  12. #12
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    OK, no problem. Glad to hear all is working well. It may not necessarily be elegant in all cases, but gSOAP is very functional. The server I wrote has served us well, to the extent that the gSOAP version we're using is rather old, and we're not going to mess with what's working

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  3. moving pointers to pointers
    By Benzakhar in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2003, 07:30 AM
  4. pointers to pointers within structs
    By Lord_azrael99 in forum C Programming
    Replies: 2
    Last Post: 08-28-2003, 04:29 AM
  5. Pointers pointers pointers...
    By SMurf in forum C Programming
    Replies: 8
    Last Post: 10-23-2001, 04:55 PM

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