Class, constructor and inherance.

Rikki Cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Oct 11 19:22:59 PDT 2015


On 12/10/15 3:14 PM, holo wrote:
> Hello
>
> I'm trying to write my first class. I want to use it as module and build
> anothers on top of it. I read that default functions attributes are not
> inherited. Is it that same for constructor? This is how my class (not
> finished) is looking right now:
>
> class credential
> {
>      auto accessKey = environment.get["AWS_ACCESS_KEY"];
>      auto secretKey = environment.get["AWS_SECRET_KEY"];
> }
>
> class sigv4 : credential
> {
>      public:
>          string method;
>          string service;
>          string host;
>          string region;
>          string endpoint;
>          string request_parameters;
>          string payload;
>
>      private:
>          const algorithm = "AWS4-HMAC-SHA256";
>
>          auto currentClock = Clock.currTime(UTC());
>          auto currentDate = cast(Date)currentClock;
>          auto curDateStr = currentDate.toISOString;
>          auto currentTime = cast(TimeOfDay)currentClock;
>          auto curTimeStr = currentTime.toISOString;
>          auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z";
>
>          auto hmac_sha256(ubyte[] key, ubyte[] msg)
>          {
>              auto hmac = hmac!SHA256(key);
>              hmac.put(msg);
>              auto digest = hmac.finish;
>
>              return digest;
>          }
>
>          auto getSignatureKey(string key, string dateStamp, string
> regionName, string serviceName)
>          {
>              ubyte[] kString = cast(ubyte[])("AWS4" ~ key);
>              auto kDate = sign(kString, cast(ubyte[])dateStamp);
>              auto kRegion = sign(kDate, cast(ubyte[])regionName);
>              auto kService = sign(kRegion,  cast(ubyte[])serviceName);
>              auto kSigning = sign(kService, cast(ubyte[])"aws4_request");
>
>              return kSigning;
>          }
>
>          auto getCanonicalRequest(string canonicalURI, string
> canonicalQueryString, string canonicalHeaderString, string signedHeaders)
>          {
>              string payloadHash = sha256Of("").toHexString.toLower;
>              string canonicalRequest = method ~ "\n" ~ canonicalURI ~
> "\n" ~ canonicalQueryString ~ "\n" ~ canonicalHeadersString ~ "\n" ~
> signedHeaders ~ "\n" ~ payloadHash;
>          }
>
>          this()
>          {
>
>          }
> }
>
> I need to set all variables by defaults values from "public" and all
> needed by function "getCanonicalRequest". But if i will use that class
> to build another should i set defaults again? If yes is there some
> solution for it?
>
> //holo

By the looks, I'm guessing you do not have much experience when it comes 
to OOP.

I think you are wanting something a bit closer to:

import std.typecons : tuple, TypeTuple;

interface Credential {
	string encode(....);
}

class SigV4 : Credential {
	this(....) {
		....	
	}
	
	string encode(....) {
		
	}
	
	private:
	....
}


TypeTuple!(string, string) AWSKeys() {
     import std.process;
     return tuple(environment.get("AWS_ACCESS_KEY"), 
environment.get("AWS_SECRET_KEY"));
}


More information about the Digitalmars-d-learn mailing list