Good style for public enum type in class with enum type in public and private function prototypes
I'm writing code that uses a public enumerated type as input to private and public functions. I have to declare the enum
type before I use it in any function prototype so I can't place it with the rest of the public
declarations. I also need to keep the public
declarations after the private
ones to conform with a style guide.
I am wondering if there is a way to only have one public
declaration section instead of two, or if there is a better way of organizing the header file.
Here is my code:
main.cpp
#include "A.h"
int main() {
const int WIDTH = 3;
const int HEIGHT = 4;
const int MAX_HEIGHT = 6;
A a = A(MAX_HEIGHT, WIDTH, HEIGHT);
for (int i = 0; i < HEIGHT; ++i) {
a.SetValue(0, i, A::VALUE_2);
a.SetValue(WIDTH - 1, i, A::VALUE_2);
}
for (int j = 0; j < WIDTH; ++j) {
a.SetValue(j, 0, A::VALUE_3);
a.SetValue(j, WIDTH - 1, A::VALUE_3);
}
for (int i = 0; i < HEIGHT - 2; ++i) {
for (int j = 0; j < WIDTH - 2; ++j) {
a.SetIntegers(j, i, i, j , i + j);
}
}
return 0;
}
A.h
#include <vector>
using namespace std;
class A {
public:
enum Enumeration {VALUE_1, VALUE_2, VALUE_3};
private:
struct Container {
int i;
int j;
int k;
};
int maxValue;
vector<vector<Container>> data;
Container CreateContainer(Enumeration enumValue) const;
public:
A(int maxValue, int xDimension, int yDimension) : maxValue(maxValue),
data(vector<vector<Container>>(yDimension, vector<Container>(xDimension, CreateContainer(VALUE_1)))) {}
~A() {data.clear();}
void SetValue(int x, int y, Enumeration enumValue);
void SetIntegers(int x, int y, int i, int j, int k);
};
A.cpp
#include "A.h"
A::Container A::CreateContainer(Enumeration enumValue) const {
Container container;
switch (enumValue) {
case VALUE_1:
container.i = 0;
container.j = maxValue;
container.k = maxValue;
case VALUE_2:
container.i = maxValue;
container.j = 0;
container.k = maxValue;
case VALUE_3:
container.i = maxValue;
container.j = maxValue;
container.k = 0;
};
return container;
}
void A::SetValue(int x, int y, Enumeration enumValue) {
data.at(y).at(x) = CreateContainer(enumValue);
return;
}
void A::SetIntegers(int x, int y, int i, int j, int k) {
data.at(y).at(x).i = i;
data.at(y).at(x).j = j;
data.at(y).at(x).k = k;
return;
}
c++
add a comment |
I'm writing code that uses a public enumerated type as input to private and public functions. I have to declare the enum
type before I use it in any function prototype so I can't place it with the rest of the public
declarations. I also need to keep the public
declarations after the private
ones to conform with a style guide.
I am wondering if there is a way to only have one public
declaration section instead of two, or if there is a better way of organizing the header file.
Here is my code:
main.cpp
#include "A.h"
int main() {
const int WIDTH = 3;
const int HEIGHT = 4;
const int MAX_HEIGHT = 6;
A a = A(MAX_HEIGHT, WIDTH, HEIGHT);
for (int i = 0; i < HEIGHT; ++i) {
a.SetValue(0, i, A::VALUE_2);
a.SetValue(WIDTH - 1, i, A::VALUE_2);
}
for (int j = 0; j < WIDTH; ++j) {
a.SetValue(j, 0, A::VALUE_3);
a.SetValue(j, WIDTH - 1, A::VALUE_3);
}
for (int i = 0; i < HEIGHT - 2; ++i) {
for (int j = 0; j < WIDTH - 2; ++j) {
a.SetIntegers(j, i, i, j , i + j);
}
}
return 0;
}
A.h
#include <vector>
using namespace std;
class A {
public:
enum Enumeration {VALUE_1, VALUE_2, VALUE_3};
private:
struct Container {
int i;
int j;
int k;
};
int maxValue;
vector<vector<Container>> data;
Container CreateContainer(Enumeration enumValue) const;
public:
A(int maxValue, int xDimension, int yDimension) : maxValue(maxValue),
data(vector<vector<Container>>(yDimension, vector<Container>(xDimension, CreateContainer(VALUE_1)))) {}
~A() {data.clear();}
void SetValue(int x, int y, Enumeration enumValue);
void SetIntegers(int x, int y, int i, int j, int k);
};
A.cpp
#include "A.h"
A::Container A::CreateContainer(Enumeration enumValue) const {
Container container;
switch (enumValue) {
case VALUE_1:
container.i = 0;
container.j = maxValue;
container.k = maxValue;
case VALUE_2:
container.i = maxValue;
container.j = 0;
container.k = maxValue;
case VALUE_3:
container.i = maxValue;
container.j = maxValue;
container.k = 0;
};
return container;
}
void A::SetValue(int x, int y, Enumeration enumValue) {
data.at(y).at(x) = CreateContainer(enumValue);
return;
}
void A::SetIntegers(int x, int y, int i, int j, int k) {
data.at(y).at(x).i = i;
data.at(y).at(x).j = j;
data.at(y).at(x).k = k;
return;
}
c++
1
We need your real working code. We don't do general reviews of hypothetical designs. This might be a good fit for Software Engineering but see their help center first.
– bruglesco
2 hours ago
add a comment |
I'm writing code that uses a public enumerated type as input to private and public functions. I have to declare the enum
type before I use it in any function prototype so I can't place it with the rest of the public
declarations. I also need to keep the public
declarations after the private
ones to conform with a style guide.
I am wondering if there is a way to only have one public
declaration section instead of two, or if there is a better way of organizing the header file.
Here is my code:
main.cpp
#include "A.h"
int main() {
const int WIDTH = 3;
const int HEIGHT = 4;
const int MAX_HEIGHT = 6;
A a = A(MAX_HEIGHT, WIDTH, HEIGHT);
for (int i = 0; i < HEIGHT; ++i) {
a.SetValue(0, i, A::VALUE_2);
a.SetValue(WIDTH - 1, i, A::VALUE_2);
}
for (int j = 0; j < WIDTH; ++j) {
a.SetValue(j, 0, A::VALUE_3);
a.SetValue(j, WIDTH - 1, A::VALUE_3);
}
for (int i = 0; i < HEIGHT - 2; ++i) {
for (int j = 0; j < WIDTH - 2; ++j) {
a.SetIntegers(j, i, i, j , i + j);
}
}
return 0;
}
A.h
#include <vector>
using namespace std;
class A {
public:
enum Enumeration {VALUE_1, VALUE_2, VALUE_3};
private:
struct Container {
int i;
int j;
int k;
};
int maxValue;
vector<vector<Container>> data;
Container CreateContainer(Enumeration enumValue) const;
public:
A(int maxValue, int xDimension, int yDimension) : maxValue(maxValue),
data(vector<vector<Container>>(yDimension, vector<Container>(xDimension, CreateContainer(VALUE_1)))) {}
~A() {data.clear();}
void SetValue(int x, int y, Enumeration enumValue);
void SetIntegers(int x, int y, int i, int j, int k);
};
A.cpp
#include "A.h"
A::Container A::CreateContainer(Enumeration enumValue) const {
Container container;
switch (enumValue) {
case VALUE_1:
container.i = 0;
container.j = maxValue;
container.k = maxValue;
case VALUE_2:
container.i = maxValue;
container.j = 0;
container.k = maxValue;
case VALUE_3:
container.i = maxValue;
container.j = maxValue;
container.k = 0;
};
return container;
}
void A::SetValue(int x, int y, Enumeration enumValue) {
data.at(y).at(x) = CreateContainer(enumValue);
return;
}
void A::SetIntegers(int x, int y, int i, int j, int k) {
data.at(y).at(x).i = i;
data.at(y).at(x).j = j;
data.at(y).at(x).k = k;
return;
}
c++
I'm writing code that uses a public enumerated type as input to private and public functions. I have to declare the enum
type before I use it in any function prototype so I can't place it with the rest of the public
declarations. I also need to keep the public
declarations after the private
ones to conform with a style guide.
I am wondering if there is a way to only have one public
declaration section instead of two, or if there is a better way of organizing the header file.
Here is my code:
main.cpp
#include "A.h"
int main() {
const int WIDTH = 3;
const int HEIGHT = 4;
const int MAX_HEIGHT = 6;
A a = A(MAX_HEIGHT, WIDTH, HEIGHT);
for (int i = 0; i < HEIGHT; ++i) {
a.SetValue(0, i, A::VALUE_2);
a.SetValue(WIDTH - 1, i, A::VALUE_2);
}
for (int j = 0; j < WIDTH; ++j) {
a.SetValue(j, 0, A::VALUE_3);
a.SetValue(j, WIDTH - 1, A::VALUE_3);
}
for (int i = 0; i < HEIGHT - 2; ++i) {
for (int j = 0; j < WIDTH - 2; ++j) {
a.SetIntegers(j, i, i, j , i + j);
}
}
return 0;
}
A.h
#include <vector>
using namespace std;
class A {
public:
enum Enumeration {VALUE_1, VALUE_2, VALUE_3};
private:
struct Container {
int i;
int j;
int k;
};
int maxValue;
vector<vector<Container>> data;
Container CreateContainer(Enumeration enumValue) const;
public:
A(int maxValue, int xDimension, int yDimension) : maxValue(maxValue),
data(vector<vector<Container>>(yDimension, vector<Container>(xDimension, CreateContainer(VALUE_1)))) {}
~A() {data.clear();}
void SetValue(int x, int y, Enumeration enumValue);
void SetIntegers(int x, int y, int i, int j, int k);
};
A.cpp
#include "A.h"
A::Container A::CreateContainer(Enumeration enumValue) const {
Container container;
switch (enumValue) {
case VALUE_1:
container.i = 0;
container.j = maxValue;
container.k = maxValue;
case VALUE_2:
container.i = maxValue;
container.j = 0;
container.k = maxValue;
case VALUE_3:
container.i = maxValue;
container.j = maxValue;
container.k = 0;
};
return container;
}
void A::SetValue(int x, int y, Enumeration enumValue) {
data.at(y).at(x) = CreateContainer(enumValue);
return;
}
void A::SetIntegers(int x, int y, int i, int j, int k) {
data.at(y).at(x).i = i;
data.at(y).at(x).j = j;
data.at(y).at(x).k = k;
return;
}
c++
c++
asked 3 hours ago
Jacob Bischoff
604
604
1
We need your real working code. We don't do general reviews of hypothetical designs. This might be a good fit for Software Engineering but see their help center first.
– bruglesco
2 hours ago
add a comment |
1
We need your real working code. We don't do general reviews of hypothetical designs. This might be a good fit for Software Engineering but see their help center first.
– bruglesco
2 hours ago
1
1
We need your real working code. We don't do general reviews of hypothetical designs. This might be a good fit for Software Engineering but see their help center first.
– bruglesco
2 hours ago
We need your real working code. We don't do general reviews of hypothetical designs. This might be a good fit for Software Engineering but see their help center first.
– bruglesco
2 hours ago
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210580%2fgood-style-for-public-enum-type-in-class-with-enum-type-in-public-and-private-fu%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210580%2fgood-style-for-public-enum-type-in-class-with-enum-type-in-public-and-private-fu%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
We need your real working code. We don't do general reviews of hypothetical designs. This might be a good fit for Software Engineering but see their help center first.
– bruglesco
2 hours ago