Gå til innhold

Hjelp til skoleoppgave, binary/source compatible


Anbefalte innlegg

Hei! Har en skoleoppgave som jeg behøver litt hjelp til å komme i gang med. Den er ganske lang, men håper noen vil ta seg tid til å hjelpe :)

C++ developers often talk about binary and source compatibility. Assume we have some class C and we make a change to it so that, now we have a newer version of the class C’: If all clients of C work just as well with C’ without even recompiling the clients, then C and C’ are binary compatible. If all clients of C work just as well with C’ without modifying the clients, but after recompiling them, then C and C’ are source compatible. If clients of C may need source code adjustments to work with C’, then C and C’ are not compatible. Well encapsulated C++ classes have a much higher chance to be binary compatible with many modifications. This is desirable, since it makes it possible to update the classes without breaking clients.
Consider the following C++ class:
// Point.h
class Point {
private:
int x_,y_;
public:
Point(int x, int y);
int x();
int y();
};
// Point.cpp
#include "Point.h"
Point::Point(int x, int y): x_{x}, y_{y} {}
int Point::x() { return x_;}
int Point::y() { return y_;}
And the following client of that class:
// Client.cpp
#include <iostream>
#include "Point.h"
int main() {
Point points[3] = {{1,2},{3,4},{5,6}};
for(auto p : points) std::cout << p.x() << p.y();
return 0;
}
The main function allocates three objects of type Point on the stack. Since main allocates the memory, it needs to know precisely how much space an object of type Point takes. This illustrates the fact that in C++, the memory layout of objects (also known as the object model) is part of the public interface of a class. Knowing this, and without modifying the client in any way, do the following:
  • Find two different ways to add something to the private part of Point, such that the new code is not binary compatible with the client, but it is still source compatible. For both cases write only the new code and provide a brief explanation why this breaks the client.

Hvis noen har noen tips til hva som kan inkluderes, så ville det vært veldig god hjelp på vei!

Lenke til kommentar
Videoannonse
Annonse

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...