C++ for Lua Developers
Transfer Lua experience to C++17: values, flow, functions, containers, headers, classes, virtual dispatch, RAII, and move ownership. Build a connected library desk.
Lua and C++ side by side
The same program in Lua and in C++, from the lessons of this course.
Strings and Output
A Lua string is an immutable byte sequence: methods such as upper return a new string, .. concatenates, and # counts bytes. C++ std::string owns a mutable byte sequence, so you can change a character in place or append with +=, and stream insertion with <<…
Lua1local name = "oak"2local loud = name:upper()3print(name .. " " .. loud .. " " .. #name)C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::string name="oak";15name[0]='O';16name+="s";17std::cout<<name<<" "<<name.size()<<"\n";18}Text Equality
Lua == compares strings by contents, while two tables are equal only when they are the same table, unless a metatable supplies __eq. In C++, what == does depends on the declared operand types. C++ std::string equality compares contents, including against a…
Lua1local a = "red"2local b = "re" .. "d"3print(a == b)4local t1 = {"red"}5local t2 = {"red"}6print(t1 == t2)C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::string a="red",b="re";15b+="d";16std::cout<<std::boolalpha<<(a==b)<<"\n";17const char* p="red";18const char* q=a.c_str();19std::cout<<(p==q)<<" "<<(a==p)<<"\n";20}Fixed-size Arrays
A Lua table used as an array starts at index 1, grows when you assign to the next index, and is shared when you assign it to another variable; reading past the end gives nil. C++ std::array<T,N> is a fixed-size value type indexed from 0. Its size is part of…
Lua1local items = {4, 8}2local alias = items3alias[#alias + 1] = 124print(#items .. " " .. items[1] .. " " .. items[#items])5print(items[5])C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::array<int,3> a{4,5,6};15auto b=a;16b[0]=9;17std::cout<<a.size()<<" "<<a[0]<<" "<<b.at(0)<<"\n";18}Value Copies
In Lua, a table is a reference value: b = a makes two names for one table, and there is no built-in copy. A copy loop over ipairs creates a new outer table, but any nested tables are still shared, so it is only a shallow copy. Numbers and strings are…
Lua1local original = {{1}}2local copy = {}3for i, v in ipairs(original) do4 copy[i] = v5end6copy[1][2] = 27copy[2] = {9}8print(#original)9print(#original[1])C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::vector<std::vector<int>> original{{1}};15auto copy=original;16copy[0].push_back(2);17copy.push_back({9});18std::cout<<original.size()<<" "<<original[0].size()<<"\n";19std::cout<<copy.size()<<" "<<copy[0].size()<<"\n";20}Functions and Returns
Lua functions are first-class values with untyped parameters. A call with too few arguments fills the rest with nil, extra arguments are dropped, and a function can return several values, of which a single assignment keeps only the first. Mistakes show up at…
Lua1local function divmod(a, b)2 return a // b, a % b3end4local q, r = divmod(17, 5)5print(q .. " " .. r)6local only = divmod(9, 4)7print(only)C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13std::pair<int,int> divmod(int a,int b){return {a/b,a%b};}14int triple(int n){return 3*n;}15int main(){16auto [q,r]=divmod(17,5);17std::cout<<q<<" "<<r<<" "<<triple(4)<<"\n";18}Streams and Exact Parsing
Lua's tonumber never raises: it returns nil for text that is not a number, such as "7x", and it accepts surrounding spaces, hexadecimal like "0x10" and floats. Splitting is done with patterns such as %S+. C++ stream extraction with >> reads as much as it can…
Lua1local word, number = ("oak 7"):match("^(%S+)%s+(%S+)$")2print(word .. "/" .. tonumber(number))3print(tonumber("7x"))4print(tonumber(" 8 ") + 1)C++1#include <iostream>2#include <string>3#include <vector>4#include <array>5#include <map>6#include <set>7#include <algorithm>8#include <memory>9#include <sstream>10#include <stdexcept>11#include <utility>12 13int main(){14std::istringstream in("oak 7x");15std::string word;int n;16if(in>>word>>n)std::cout<<word<<"/"<<n<<"\n";17std::string rest;18if(in>>rest)std::cout<<"trailing "<<rest<<"\n";19std::istringstream bad("x9");20int m=5;21if(!(bad>>m))std::cout<<"failed "<<m<<"\n";22}
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsC++ Files and Values
6 lessonsConditions and Iteration
5 lessonsStandard Containers
5 lessonsCopies and Algorithms
5 lessonsFunctions and Templates
5 lessonsParsing and Lifetime
5 lessonsObjects and Invariants
6 lessonsComposition and Ownership
5 lessonsProject: Library Desk
4 lessonsFinal Challenges
2 lessonsFrequently asked questions
Who is C++ for Lua Developers for?
For people who already write Lua and want to learn C++. Each lesson starts from Lua you know and shows how C++ does the same thing, so you skip the beginner basics.
Do I need to know C++ already?
No. The course starts with your first C++ program. You need Lua basics: variables, conditions, loops, functions and collections.
How many lessons does C++ for Lua Developers have?
C++ for Lua Developers contains 48 interactive lessons you can complete at your own pace.
What does C++ for Lua Developers cover?
The course is organized into chapters covering: C++ Files and Values, Conditions and Iteration, Standard Containers, Copies and Algorithms, and more.
Is C++ for Lua Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.