in Education by
Is there a side effect in doing this: C code: struct foo { int k; }; int ret_foo(const struct foo* f){ return f.k; } C++ code: class bar : public foo { int my_bar() { return ret_foo( (foo)this ); } }; There's an extern "C" around the C++ code and each code is inside its own compilation unit. Is this portable across compilers? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
This is entirely legal. In C++, classes and structs are identical concepts, with the exception that all struct members are public by default. That's the only difference. So asking whether you can extend a struct is no different than asking if you can extend a class. There is one caveat here. There is no guarantee of layout consistency from compiler to compiler. So if you compile your C code with a different compiler than your C++ code, you may run into problems related to member layout (padding especially). This can even occur when using C and C++ compilers from the same vendor. I have had this happen with gcc and g++. I worked on a project which used several large structs. Unfortunately, g++ packed the structs significantly looser than gcc, which caused significant problems sharing objects between C and C++ code. We eventually had to manually set packing and insert padding to make the C and C++ code treat the structs the same. Note however, that this problem can occur regardless of subclassing. In fact we weren't subclassing the C struct in this case.

Related questions

0 votes
    The code below copies data from one vector into another. If the vectors are large then I guess this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    The code below copies data from one vector into another. If the vectors are large then I guess this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I would like to use CHOLMOD's GPU acceleration, and have found several simple examples on how to use ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I have a struct A that is defined as follows: typedef struct A { CvRect B; // rect int C; ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
+1 vote
    Is it possible to use curly brackets ({}) to enclose a single line code in C program?...
asked Nov 8, 2020 in Technology by JackTerrance
0 votes
    I have written a piece of code to validate a keyword, it validates and makes sure that the word is 5 letters long and ... the problem, as it works fine without it. The code: cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I've just started writing unit tests for a legacy code module with large physical dependencies using the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    I've just started writing unit tests for a legacy code module with large physical dependencies using the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I'm relatively new to C++, and I'm hoping someone can help me resolve an issue I'm having with unique_ptr and vectors. ... ) { useLegs(); } void Biped::useLegs() { std::cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I have a function value(x) which is overloaded for many types such that: double value(double x) { return x; } double ... [-Wreturn-local-addr] in the following code: Matrix33 m; m...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    When should we use pointers in a C program?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    As it currently stands, this question is not a good fit for our Q&A format. We expect answers to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    If you have an existing package that doesn't have an .Rproj file, you can use devtools for the use_rstudio ... of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Having two thread, one product data, another one process data. The data is not just an int or float but a complex object. In ... = buffer; //mutex.unlock(); if(!tmp.data) std::cout...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    Which of the following code drop the ith and jth column? (a) new...
asked Feb 12, 2022 in Education by JackTerrance
...