Implementing Interfaces

Hi there, total C++ newb.

so I have a header file that is set up like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
namespace whatever{
  
   classA
   {
      public:
          virtual void classAMethod();
   }

   classB
   {
      public:
           virtual void classBMethod();
   }

   classC
   {
      public:
            classC(classA* arg1, classB* arg2);
            virtual void classCMethod();
      private:
            classB* classBvariable;
            classA* classAVariable
   }
}


I have a cpp file that implemented this garbage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

namespace whatever{

   classC::classC(classA* arg1, classB* arg2) : arg1(arg1), arg2(arg2)
   {

   }

   classB* classB::classBMethod()
   {
       NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
                    
                    request = (NSMutableURLRequest)classAVariable; // problem here. compiler error: use of undeclared identifier 'classAVariable'

   }
}


There are some other methods that are not shown here. But the idea is that I have a method that creates a HTTP Request and returns pointer to classAVariable
Then classAVariable is used by another method to complete the Web Request. I know that some of this is abstract and convoluted. but I inherited these interfaces, not really happy about the set up.

So really the classAVariable is a global variable that I would like to share throughout classA, classB, and classC.

Normally my domain is C#. Can someone please point out the error in my ways!
Couldn't you pass the classAVariable to the method then? It seems strange to me that you would have several classes that interoperate on a global variable to work properly.
Hi Zhuge thanks for the reply.

you simply mean:

classB* classB::classBMethod(classA* classAVariable)
{
// do some work :)
}

I thought about that. I'm pretty sure that would work. Is that the best way to implement this? Just curious, pretty much anyone here has more exp than myself in C++

Without knowing why you need this variable I can't really give you any more details. Like I said before, it seems odd to have a member that all the classes need in order to work. If, from the method's perspective, its just some random data, then there should be no issue in terms of good/bad practice.
hopefully this will shed some light

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// the https response
                class HttpsResponse
                {
                public:
                    virtual std::string getHeaderValue(std::string key);
                    virtual std::string getContentAsString();
                };
                // base class for httpsRequest
                class HttpsRequest
                {
                public:
                    virtual void setHeaderValue(std::string key, std::string value);
                    virtual HttpsResponse* submit(); //caller takes ownership of result
                };
                // post request inherits from https request
                class HttpsPostRequest : public HttpsRequest
                {
                public:
                    virtual Uploader* UploadStream(); //caller takes ownership of result
                };
                
                class WebCommunication
                {
                public:
                    WebCommunication(HttpsRequest* request, HttpsPostRequest* postRequest);
                    virtual HttpsPostRequest* makeHttpsPostRequest(std::string uri); //caller takes ownership of result
                    virtual HttpsRequest* makeHttpsGetRequest(std::string uri); //caller takes ownership of result
                private:
                    AppleHttpsRequest *appleRequest; // not sure I need these
                    AppleHttpsPostRequest *applePostRequest; // not sure I need these
                };


the .cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
WebCommunication::WebCommunication(HttpsRequest* request, HttpsPostRequest* postRequest)
                :request(request), 
                PostRequest(postRequest)
                {
                    
                }
                                
                std::string HttpsResponse::getHeaderValue(std::string key)
                {
                    
                    
                }
                
                std::string HttpsResponse::getContentAsString()
                {
                    
                }
                
                void HttpsRequest::setHeaderValue(std::string key, std::string value)
                {
                    
                }
                
                
                
                
                // this just creates an HttpsPostRequest
                HttpsPostRequest* WebCommunication::makeHttpsPostRequest(std::string uri)
                {
                    NSString *urlString = [NSString stringWithFormat:@"%@", uri.c_str()];
                    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
                    [request setURL:[NSURL URLWithString:urlString]];
                    [request setHTTPMethod:@"POST"];
                    
                    // should set the headers
                    NSString *contentType = [NSString stringWithFormat:@"text/xml"];
                    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
                    
                    //create the body - will the body be just raw bytes?
                    NSMutableData *postBody = [NSMutableData data];
                    [postBody appendData:[[NSString stringWithFormat:@"<xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
                    [postBody appendData:[[NSString stringWithFormat:@"<yourcode/>"] dataUsingEncoding:NSUTF8StringEncoding]];
                    [postBody appendData:[[NSString stringWithFormat:@"</xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
                    
                    [request setHTTPBody:postBody];
                    
                    postRequest = (HttpsPostRequest*)request;
                    return postRequest;
                }
                
                // Creates Https Get Request
                HttpsRequest* WebCommunication::makeHttpsGetRequest(std::string uri)
                {
                    NSString *urlString = [NSString stringWithFormat:@"%@", uri.c_str()];
                    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
                    [request setURL:[NSURL URLWithString:urlString]];
                    [request setHTTPMethod:@"GET"];
                    
                    // should set the headers
                    NSString *contentType = [NSString stringWithFormat:@"text/xml"];
                    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
                    
                    //create the body - will the body be just raw bytes?
                    NSMutableData *postBody = [NSMutableData data];
                    [postBody appendData:[[NSString stringWithFormat:@"<xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
                    [postBody appendData:[[NSString stringWithFormat:@"<yourcode/>"] dataUsingEncoding:NSUTF8StringEncoding]];
                    [postBody appendData:[[NSString stringWithFormat:@"</xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
                    
                    [request setHTTPBody:postBody];
                    
                    //HttpsPostRequest *request = (HttpsPostRequest*)request;
                    request = (HttpsRequest*)request;
                    
                    return appleRequest;

                }
                
                // this would simply submit the request and return the response data
                HttpsResponse* HttpsRequest::submit()
                {
                    
                    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
                    
                    request = (NSMutableURLRequest)request;
                    
                    
                    NSHTTPURLResponse* urlResponse = nil;  
                    NSError *error = [[NSError alloc] init];  
                    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
                    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
                    
                    NSLog(@"Response Code: %d", [urlResponse statusCode]);
                    if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
                        NSLog(@"Response: %@", result);
                    }
                    //here you get the response    
                    
                    // wrap the raw response in AppleHttpsResponse not sure why
                    HttpsResponse *response_data = (HttpsResponse*)urlResponse;
                    
                    return response_data; // will have to cast this back to HttsResponse
                }


I have been looking at this for a few hours. Maybe I'm just implementing this completely incorrectly
Topic archived. No new replies allowed.